Objective c 如何使用字典数组加载tableView?
我想用一个本身有多个字典的数组加载一个表视图。这些字典中的每一个都有我需要放入一行的条目,每行一个字典。所有这些字典都存储在一个数组中 我应该如何执行此任务 谢谢 请参见下面的代码,projects是我的数组Objective c 如何使用字典数组加载tableView?,objective-c,cocoa-touch,ios,uitableview,Objective C,Cocoa Touch,Ios,Uitableview,我想用一个本身有多个字典的数组加载一个表视图。这些字典中的每一个都有我需要放入一行的条目,每行一个字典。所有这些字典都存储在一个数组中 我应该如何执行此任务 谢谢 请参见下面的代码,projects是我的数组 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRow
- (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];
}
-(NSInteger)节数表视图:(UITableView*)表视图{
返回1;
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节{
//id sectionInfo=[[self.fetchedResultsController节]objectAtIndex:section];
返回self.projects.count;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
静态NSString*CellIdentifier=@“Cell”;
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
如果(单元格==nil){
cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault重用标识符:CellIdentifier]自动释放];
}
//配置单元格。
[self-configureCell:cell-atIndexPath:indexPath];
返回单元;
}
-(void)configureCell:(UITableViewCell*)单元格atIndexPath:(NSIndexPath*)indexPath{
cell.textlab.text=[self.projects objectAtIndex:indexath.row];
}
你可以用你所拥有的东西来做这件事。您只需将configureCell:atIndexPath:
方法更改为如下所示:
cell.textLabel.text = [[self.projects objectAtIndex:indexPath.row] objectForKey:@"some_key"];
只需将some_key
更改为NSDictionary
中要显示在UITableViewCell
中的键即可
您还可以将
UITableViewCell
样式更改为UITableViewCellStyleSubtitle
,然后您可以将NSDictionary
中的另一个键添加到单元格中。detailTextLabel
您可以利用现有的功能完成此操作。您只需将configureCell:atIndexPath:
方法更改为如下所示:
cell.textLabel.text = [[self.projects objectAtIndex:indexPath.row] objectForKey:@"some_key"];
只需将some_key
更改为NSDictionary
中要显示在UITableViewCell
中的键即可
您还可以将
UITableViewCell
样式更改为UITableViewCellStyleSubtitle
,然后可以将NSDictionary
中的另一个键添加到单元格。除了edc1591之外,还可以添加detailTextLabel
如果数组中的字典具有不同的键,则
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];
}
除了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];
}
你能添加一些代码到你被卡住的地方吗?你能添加一些代码到你被卡住的地方吗?