如何在处理UITableView时访问Array中Array内的Array

时间:2016-04-05 14:08:33

标签: ios objective-c arrays uitableview enums

这是我的结构的NSLog(每个枚举是一个自定义的tableViewCell)。我需要能够在表格视图中显示每个单元格.-

(
    (
    0,
    0
),
    (
    4
),
    (
    1,
            (
                    (
            2
        ),
                    (
            2
        ),
                    (
            2
        ),
                    (
            2
        ),
                    (
            2
        ),
                    (
            2
        ),
                    (
            2
        ),
                    (
            2
        )
    )
),
    (
    4
)
)

如您所见,它是一个包含4个Array对象的Array。在这些对象中是枚举或带枚举的数组。 [0]有一个包含两个0枚举的数组,[1]有一个带有4个枚举的数组,[2]有一个带有1个枚举的数组,而一个数组内的数组有8个枚举为2。

我的问题是如何在

中显示这些单元格
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

BucketListCellType type = [[[[self.presentedCells objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] objectAtIndex:0] intValue];

}

给我一​​个错误 - [__NSCFNumber objectAtIndex:]: unrecognized selector sent to instance

这是我构建该数组的地方 -

-(void)showPresentedCells
{ 

NSMutableArray *preCells = [NSMutableArray new];

// First Section - Suggested Bucket List Images

NSMutableArray *suggestedBucketListImages = [NSMutableArray new];

[suggestedBucketListImages addObject:@(mainPhotoCellWithAddToBucketList)];
[suggestedBucketListImages addObject:@(mainPhotoCellWithAddToBucketList)];

[preCells addObject:[NSArray arrayWithArray:suggestedBucketListImages]];

// Second Section - Top Bucket List Images

NSMutableArray *topBucketListImages = [NSMutableArray new];

[topBucketListImages addObject:@(mainPhotoCell)];

[preCells addObject:[NSArray arrayWithArray:topBucketListImages]];

// Third Section - My Bucket List Images

NSMutableArray *myBucketListCategories = [NSMutableArray new];
NSMutableArray *categoryWithPictures = [NSMutableArray new];

[myBucketListCategories addObject:@(CreateNewBucketListCell)];

for (int i = 0; i <= [self.bucketListCategories count]; i++) {

    [categoryWithPictures addObject:@[@(CategoryCell)]];

}

[myBucketListCategories addObject:[NSArray arrayWithArray:categoryWithPictures]];

[preCells addObject:[NSArray arrayWithArray:myBucketListCategories]];


NSMutableArray *unorganizedBucketListImages = [NSMutableArray new];

[unorganizedBucketListImages addObject:@(mainPhotoCell)];
[preCells addObject:[NSArray arrayWithArray:unorganizedBucketListImages]];

self.presentedCells = preCells;

}

感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

问题的根源在于您正在为三个级别的数组调用objectAtIndex,但这只会在您真正拥有三个级别的结构时才会起作用(例如2部分,行1)。在其他任何地方,您只有两个级别的结构(返回NSNumber)并尝试使用它来调用objectAtIndex将产生您描述的错误。

对于我来说不太清楚的是,当只有两个级别的结构时,你想要展示的是什么。但你可能会做类似的事情:

BucketListCellType type;
id object = self.presentedCells[indexPath.section][indexPath.row];

if ([object isKindOfClass:[NSArray class]]) {
    type = [object[0] intValue];
} else {
    type = [object intValue];