2015-12-30 65 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; 

}

相關問題