UITableView - 使用一些复杂的数据填充表

时间:2016-02-11 23:07:26

标签: ios objective-c uitableview nsmutablearray nsmutabledictionary

好的,我有一些复杂的数据需要放在自定义表视图中。以下是NSMutableDictionary

的结构
cellData = [ "Section Header title" : NSMutableArray]; // NSMutableArray has custom cell objects

所以,基本上我是用自己的相应章节标题名称标记自定义单元格对象的数组。问题是,我无法将数据设置为表格视图单元格。 到目前为止,我一直在cellForRowAtIndexPath

处理......
HomeTableViewCell *cell = (HomeTableViewCell*) [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
NSString *sectionTitle = [sectionNames objectAtIndex:indexPath.row];

NSMutableArray *array = [cellData objectForKey:sectionTitle];

if ([sectionTitle isEqual:@"Email"]) {
    Email *email = (Email*) [arr objectAtindex:indexPath.row]; // I know this is wrong because there're multiple objects in "array". I couldn't get it right
    [cell setCellData:...];
}

仅使用indexPath.row我就无法获得" array"中的所有对象。任何尝试使用字典值填充自定义表格视图单元格的任何人("键":"数组")?

1 个答案:

答案 0 :(得分:3)

我认为你想要做的是在获取节标题时使用indexPath.section:

NSString *sectionTitle = [sectionNames objectAtIndex:indexPath.section];

一旦你有正确的部分,你的数据应该正确排列。

当然,这假设您正确设置了其他方法,即:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
     return sectionNames.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSString *sectionTitle = [sectionNames objectAtIndex:indexPath.row];
    NSMutableArray *array = [cellData objectForKey:sectionTitle];
    return array.count;

}