我想訪問其中一個數組並顯示所選特定數組的所有內容。iOS僅在一個二維數組陣列中訪問
這是代碼:
老錯誤的代碼片段
NSArray *prova = [[NSArray alloc] initWithObjects:@"http://i.imgur.com/3IBezow.jpg", nil];
_BIGArray = [[NSArray alloc] initWithObjects:prova, prova1, nil];
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
// Configure the cell
cell.gridImage.image = [UIImage imageNamed:@"loading.png"];
url = [[_BIGArray objectAtIndex:_idArray]objectAtIndex:_idArray];
cell.gridImage.associatedObject = url;
[self.operationManager GET:url
parameters:nil
success:^(AFHTTPRequestOperation *operation, id responseObject) {
if ([cell.gridImage.associatedObject isEqualToString:url])
{
cell.gridImage.image = responseObject;
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Failed with error %@.", error);
}];
return cell;
}
更新的代碼
collectionviewcontroller.h
@property (nonatomic, strong) NSArray *BIGArray;
@property (nonatomic, retain) AFHTTPRequestOperationManager *operationManager;
@property (nonatomic, assign) NSInteger idArray;
collectionviewcontroller.m
NSArray *prova = [[NSArray alloc] initWithObjects: @"http://i.imgur.com/3IBezow.jpg", nil];
NSArray *prova1 = [[NSArray alloc] initWithObjects: @"http://i.imgur.com/yE4egi4.jpg", nil];
_BIGArray = @[prova, prova1];
- (AFHTTPRequestOperationManager *)operationManager
{
if (!_operationManager)
{
_operationManager = [[AFHTTPRequestOperationManager alloc] init];
_operationManager.responseSerializer = [AFImageResponseSerializer serializer];
};
return _operationManager;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
// Configure the cell
cell.gridImage.image = [UIImage imageNamed:@"loading.png"];
NSString *url = [_BIGArray objectAtIndex:_idArray];
cell.gridImage.associatedObject = url;
[self.operationManager GET:url
parameters:nil
success:^(AFHTTPRequestOperation *operation, id responseObject) {
if ([cell.gridImage.associatedObject isEqualToString:url])
{
cell.gridImage.image = responseObject;
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Failed with error %@.", error);
}];
return cell;
}
如果我寫這行代碼:
NSString *url = [_BIGArray objectAtIndex:_idArray];
我獲得以下錯誤:
2014-10-07 05:09:29.444 prova_prj[497:70b] ID: 0
2014-10-07 05:09:29.454 prova_prj [497:70b] -[__NSArrayI length]: unrecognized selector sent to instance 0x7b1a9400
2014-10-07 05:09:29.464 prova_prj [497:70b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI length]: unrecognized selector sent to instance 0x7b1a9400'
而且我的目標是選擇陣列(使用索引查看我從前面的視圖中獲得)並顯示所有圖像。 Objective-C有可能嗎?在Java和C#中,我沒有遇到任何重大問題。
SOLUTION
NSMutableArray *array = [[NSMutableArray alloc]initWithCapacity:2];
[array insertObject:[NSMutableArray arrayWithObjects:@"http://i.imgur.com/3IBezow.jpg", @"http://i.imgur.com/HyDJrDX.jpg", nil] atIndex: 0];
[array insertObject:[NSMutableArray arrayWithObjects:@"http://i.imgur.com/yE4egi4.jpg", @"http://i.imgur.com/twc1Ml5.jpg", @"http://i.imgur.com/66V4dhe.jpg", nil] atIndex:1];
然後,我已經改變了_BIGArray串碼:
_BIGArray = [array objectAtIndex:_idArray];
然後我改變的代碼URL字符串:
NSString *url = self.BIGArray[indexPath.row];
謝謝大家。
你可以使用一個for循環從1到數組的末尾取所有元素......但是pheraphs我錯過了你的問題。 – 2014-10-07 10:14:57
...無論如何,哪裏是二維數組?因爲在你的代碼片段中你沒有任何,只是一個簡單的一維數組。 – holex 2014-10-07 10:49:22
是的,你是正確的,只有一個陣列,但因爲我很着急,我認爲他採取了兩個去,我更新了陣列 – Matteo 2014-10-07 11:49:05