如何使用字典数组加载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];
   }