问题描述
|
我想用本身具有多个字典的数组加载表视图。这些字典中的每一个都有我需要排成一行的项目,每行一个字典。所有这些字典都存储在一个数组中。
我应该如何执行此任务?
谢谢,
参见下面的代码,项目是我的数组。
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
return self.projects.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static Nsstring *CellIdentifier = @\"Cell\";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell.
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
cell.textLabel.text = [self.projects objectAtIndex:indexPath.row];
}
解决方法
您可以用几乎所有的东西做到这一点。您只需要更改
configureCell:atIndexPath:
方法,使其看起来像这样:
cell.textLabel.text = [[self.projects objectAtIndex:indexPath.row] objectForKey:@\"some_key\"];
只需将some_key
更改为要在UITableViewCell
中显示的NSDictionary
中的键即可。
您也可以将UITableViewCell
样式更改为UITableViewCellStyleSubtitle
,然后可以将其他键从NSDictionary
添加到ѭ9key。
, 除了edc1591,
如果数组中的字典具有不同的键,则
if([[self.projects objectAtIndex:indexPath.row] count]==1)
{
id key= [[[self.projects objectAtIndex:indexPath.row] allKeys] objectAtIndex:0];
cell.textLabel.text = [[self.projects objectAtIndex:indexPath.row] objectForKey:key];
}