iOS仅在一个二维数组的数组中访问

时间:2014-10-07 10:09:32

标签: ios objective-c arrays

我想访问其中一个数组并显示所选特定数组的所有内容。

这是代码:

旧代码和错误代码

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#中我做到了没有任何重大问题。


解决方案

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];


谢谢大家。

2 个答案:

答案 0 :(得分:1)

您可以将数组作为顶部数组的第一个元素进行访问。像

NSArray *urls = (NSArray *)[_BIGArray objectAtIndex:_idArray];

答案 1 :(得分:0)

_BIGArray中只有一个对象是prova。我不知道如何检索[1] [1]甚至[1]。 您可以检索_BIGArray[0][x](其中x的范围为0到prova.count-1)或_BIGArray[0](这将为您提供数组提示)。

编辑:

NSArray *first = _BIGArray[0]会给你阵列提示 NSArray *second = _BIGArray[1]将为您提供数组prova1。

这应该做你需要的。