iphone - 在 UITableView 中显示 JSON 数据

标签 iphone ios xcode json uitableview

我的应用程序正在加载一些 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]];


}

UITableView:

-(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;

}

更清楚地说,我需要在 usernameString.text 上显示此 [testeDictionary objectForKey:@"id"]

最佳答案

您没有存储 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 值。

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

关于iphone - 在 UITableView 中显示 JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10678414/

相关文章:

ios - NSFileManager 创建目录错误 518 NSFileWriteUnsupportedSchemeError

iphone - 以类似网格的方式布局不同尺寸图像的算法

ios - 在 swift 4.0 中点击 Collection View 单元格时更改标签的文本

xcode - “copy bundle resources”中缺少 Storyboard

ios - 如何在 Xcode 中调试 EXC_BREAKPOINT (SIGTRAP)

iphone - iPhone 中 NSMutableArray 的排序

ios - 在 textFieldShouldBeginEditing 委托(delegate)中获取键盘大小

iOS/Xcode——drawRect : Not Being Called in UIView Subclass

ios - 如何在 parse.com 重新创建已删除的安装表

ios - ScrollView 中同时居中和滚动的约束