English 中文(简体)
在 UIT 视图中显示 JSON 数据
原标题:Display JSON data in UITableView

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

获取数据( 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]];


}

Uitable View :

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

}

更明确地说,我需要在 [testeDigitary 物件ForKey:@“id”] 上显示 用户名String. text ?

问题回答

您没有保存 ID

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

我猜你想做的就是这个样子

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

<强度 > EDIT(解释)

在您的方法 < code> fessededData: 中,您提取了 id,并将某些标签的文本设置到 id 上。

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

在此之后,您将忘记 id 。 然后您将提取用户名, 创建一个包含用户名的字典, 其中只包含 < 坚固 > < /坚固 > 的用户名, 并将该字典添加到名为 < code> miAray 的阵列中 。

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

<强度 > 通知您没有指定任何名为“ id” 的密钥 。

稍后,您从 miArray 获取一个字典。 此字典是您仅用一个密钥创建的词典, 即“ 用户名 ” 。 您告诉它要获得密钥“ id” 的对象, 但是由于您从未指定该密钥, 您就会得到一个 < code> nil 值 。

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





相关问题
How to change out-of-focus text selection color in Xcode?

Okay, I ll bite. I ve got really pleasant code/window colors set up in Xcode. Ordinarily, my selection color is very visible. When I am doing a project search and iterating through the results, ...

Iphone NSTimer Issue

Hi I am new to objective c. I am trying to make an app for iphone. I have a button on my view, and the click on which the function playSound is called. This is working properly. It does plays the ...

Include a .txt file in a .h in C++?

I have a number of places where I need to re-use some template code. Many classes need these items In a .h could I do something like: #include <xxx.txt> and place all of this code in the ....

Iterating over string/strlen with umlauted characters

This is a follow-up to my previous question . I succeeded in implementing the algorithm for checking umlauted characters. The next problem comes from iterating over all characters in a string. I do ...

Xcode open two editor windows with same file

Is it possible to open the same file in two separate windows in Xcode. I can open a file in one window and the same file in the main Xcode editor window, but I wanted two separate fulltime editor ...

Forcing code signing refresh in Xcode

In our environment, we share resources across multiple projects and platforms. When building for iPhone, only a subset of those resources are needed. Since that subset is still considerable, we have ...

热门标签