【发布时间】: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 循环有什么用。你为什么写这个?
标签: ios objective-c arrays uitableview dictionary