2015-12-30 63 views
-1

我有一个字典,声明为"res"。此"res"包含JSON数据的响应。iOS如何将字典键显示为表格单元格

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

当我想在tableview中显示每个id时,事实证明它只是反复显示相同的id。

这里是我的tableview:

- (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” 的资源:

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?

+1

什么的cellForRowAtIndexPath里面使用的for循环。你为什么写这些? –

回答

2

设置allKeys

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

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

e.g

- (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; 
} 
+1

for(int i = 0; i 这段代码的含义较少..为什么我们需要循环? for循环意思不大,我们可以通过使用 cell.textLabel.text = [NSString stringWithFormat:@“ID:%@”,[array objectAtIndex:indexPath.row]]; –

+0

@Dev_Tandel我很抱歉,我复制了他的'cellForRowAtIndexPath'代码并更新了我想要的内容,请参阅最新的答案 –

+0

..现在完美了。 –

0

替换为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]]; 
0

Store中的@ “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; 
} 
0

与线

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

你的循环逻辑是不正确的要达到的目标替换下面

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

线。我们有tableView的默认indexPath对象来获取tableview中的当前行,因此从您的数组中获取您在tableview的indexPath处的值。

0

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; 

}

相关问题