0

我的应用程序正在加载一些 JSON 数据,一切正常,但是当我尝试在 UITableView 单元格中显示这些数据时,什么也没有发生。我的代码如下:

获取数据(JSON):

-(void)fetchedData:(NSData *)responseData {

    NSError* error;
    NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
    NSArray* latestLoans = [json objectForKey:@"loans"];

    testeDictionary = [latestLoans objectAtIndex:0];

    testeLabel.text = [NSString stringWithFormat:@"%@",[testeDictionary objectForKey:@"id"]];

    testeString = [testeDictionary objectForKey:@"username"];
    [miArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:testeString,@"username",nil]];


}

界面视图:

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


    UITableViewCell *cell = (UITableViewCell *)[self.settingsTableView dequeueReusableCellWithIdentifier:@"CellD"];
    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CellD" owner:self options:nil];
        cell = (UITableViewCell *)[nib objectAtIndex:0];
    }


    if ([indexPath row] == 0) {

        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CellA" owner:self options:nil];
        cell = (UITableViewCell *)[nib objectAtIndex:0];


        NSDictionary *itemAtIndex = (NSDictionary *)[miArray objectAtIndex:indexPath.row];


        UILabel *usernameString = (UILabel *)[cell viewWithTag:1];
        usernameString.text = [itemAtIndex objectForKey:@"id"]; <== MUST DISPLAY JSON VALUE


    }

    return cell;

}

更清楚我需要[testeDictionary objectForKey:@"id"]usernameString.text?

4

1 回答 1

1

您没有存储 id

[miArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:testeString,@"username",nil]];

我想你想做的是这样的

NSString *idString = [NSString stringWithFormat:@"%@", [testeDictionary objectForKey:@"id"]];
[miArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:
                                          testeString, @"username",
                                          idString, @"id", 
                                          nil]];

编辑(解释)

在您的方法fetchedData:中,您提取 id 并将某些标签的文本设置为 id。

testeLabel.text = [NSString stringWithFormat:@"%@",[testeDictionary objectForKey:@"id"]];

之后你忘记了id。然后,您继续提取用户名并创建一个包含用户名的字典,并将该字典添加到名为miArray.

[miArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:testeString,@"username",nil]];

请注意,您没有指定任何名为“id”的键。

稍后,您从miArray. 这个字典是你用一个键创建的,即“用户名”。您告诉它为键“id”获取一个对象,但由于您从未指定该键,因此您会得到一个nil值。

底线,试试我的解决方案。

于 2012-05-21T01:39:10.633 回答