如何解决如何使用字典数组加载tableView?
| 我想用本身具有多个字典的数组加载表视图。这些字典中的每一个都有我需要排成一行的项目,每行一个字典。所有这些字典都存储在一个数组中。 我应该如何执行此任务? 谢谢, 参见下面的代码,项目是我的数组。- (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];
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。