iOS如何将字典键显示到表格单元格中

Posted

技术标签:

【中文标题】iOS如何将字典键显示到表格单元格中【英文标题】:iOS how to display dictionary keys into table cells 【发布时间】:2015-12-30 07:09:41 【问题描述】:

我有一本声明为"res" 的字典。这个"res" 包含 JSON 数据的响应。

我做了一个valueforkey = "id" 来检索 id 列表。

当我想将每个 id 显示到 tableview 中时,结果发现它只会重复显示相同的 id。

这是我的表格视图:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 

    return 1;


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 

    return [res count];



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

    UITableViewCell *cell = [tableViews dequeueReusableCellWithIdentifier:@"cell"];
    cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
    cell.textLabel.numberOfLines = 0;
    cell.textLabel.textColor =[UIColor blackColor];
    cell.textLabel.font = [UIFont systemFontOfSize:17.0];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    for(int i = 0; i<res.count; i++)
    cell.textLabel.text = [NSString stringWithFormat:@"ID: %@",[res  valueForKey:@"id"]];

    return cell;

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section

    UIView *view = [[UIView alloc] init];

    return view;

当我 NSLOG 输出值为“id”的 res 时:

icon = [res valueForKey:@"id"];
    NSLog(@"icon: %@", icon);

它显示:

icon: (
    300122,
    300228,
    300235,
    300272,
    300265,
    300242,
    300198,
    300135,
    300282,
    300255,
    300245,
    300248,
    300185,
    300118,
    300275,
    300295,
    300005,
    300062,
    300068,
    300055,
    300095,
    300008,
    300018,
    300015,
    300058,
    300012,
    300085,
    300038,
    300105,
    300002,
    300072,
    300022,
    300025,
    300028,
    300035,
    300258,
    300088,
    300262,
    300128,
    300215,
    300142,
    300078,
    300205,
    300315,
    300238,
    300302,
    300232,
    300285,
    300132,
    300102
)

问题是如何在 tableview 的每个单元格上显示每个 id?

【问题讨论】:

cellForRowAtIndexPath 内部的 for 循环有什么用。你为什么写这个? 【参考方案1】:

设置一个allKeys数组

NSArray *array = [NSArray arrayWithArray:[yourDictionary allKeys]];'

然后只需使用此数组设置您的 tableView dataSource

例如

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

UITableViewCell *cell = [tableViews dequeueReusableCellWithIdentifier:@"cell"];
cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
cell.textLabel.numberOfLines = 0;
cell.textLabel.textColor =[UIColor blackColor];
cell.textLabel.font = [UIFont systemFontOfSize:17.0];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.text = [NSString stringWithFormat:@"ID: %@",[array objectAtIndex:indexPath.row]];

return cell;

【讨论】:

for(int i = 0; i 这段代码意义不大..为什么我们需要循环??? for 循环的含义更少,我们只需使用 cell.textLabel.text = [NSString stringWithFormat:@"ID: %@",[array objectAtIndex:indexPath.row]]; @Dev_Tandel 对不起,我复制了他的cellForRowAtIndexPathcode 并在我想要的地方更新了,请查看更新后的答案 ..是的,现在完美了。【参考方案2】:

将 numberOfRowsInSection 替换为:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 

    NSArray *arrIds = (NSArray *)[res valueForKey:@"id"];
    return arrIds.count;

并替换

for(int i = 0; i<res.count; i++)
    cell.textLabel.text = [NSString stringWithFormat:@"ID: %@",[res  valueForKey:@"id"]];

NSArray *arrIds = (NSArray *)[res valueForKey:@"id"];
cell.textLabel.text = [NSString stringWithFormat:@"ID: %@",[arrIds objectAtIndex:indexPath.row]];

【讨论】:

【参考方案3】:

将您的 @"id" 值存储在一个数组中,如下所示;

NSArray *icon = [res valueForKey:@"id"];
    NSLog(@"icon: %@", icon); 

然后使用该数组在 TableView 单元格中显示值,如下所示;

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

UITableViewCell *cell = [tableViews dequeueReusableCellWithIdentifier:@"cell"];

cell.textLabel.text = [NSString stringWithFormat:@"ID: %@",[icon objectAtIndex:indexPath.row]];

return cell;

【讨论】:

【参考方案4】:

替换下面的行

for(int i = 0; i<res.count; i++)
cell.textLabel.text = [NSString stringWithFormat:@"ID: %@",[res  valueForKey:@"id"]];

带线

cell.textLabel.text = [NSString stringWithFormat:@"ID: %@",[[res  valueForKey:@"id"] objectAtIndex:indexPath.row]];

您的 for 循环逻辑不正确,无法实现目标。我们有 tableView 的默认 indexPath 对象来获取 tableview 中的当前行,所以从你的数组中获取 tableview 的 indexPath 的值。

【讨论】:

【参考方案5】:

ViewDidload
NSArray * iconArray = [res allKeys];
 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

UITableViewCell *cell = [tableViews dequeueReusableCellWithIdentifier:@"cell"];
cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
cell.textLabel.numberOfLines = 0;
cell.textLabel.textColor =[UIColor blackColor];
cell.textLabel.font = [UIFont systemFontOfSize:17.0];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;


cell.textLabel.text = [NSString stringWithFormat:@"ID: %@",[iconArray   objectAtIndex:indexPath.row]];

return cell;

【讨论】:

以上是关于iOS如何将字典键显示到表格单元格中的主要内容,如果未能解决你的问题,请参考以下文章

在 ios 的表格视图单元格中显示图像

EXCEL中,如何引用一个单元格中的数据,作为另一个单元格内容中的一部分?

如何在 iOS 的表格单元格中显示日期选择器和选择器视图?

如何在swift ios中将多个图像添加到自定义表格视图单元格?

如何将添加到Google表格单元格中的图像保存到Google云端硬盘?

突出显示时,表格单元格中的 iOS 自定义按钮变暗