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


}

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;

}

より明確に[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]];

EDIT(説明)

メソッドfetchedData:では、ID を抽出し、ラベルのテキストを ID に設定します。

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

その後、IDを忘れます。次に、ユーザー名の抽出に進み、ユーザー名のみを含む辞書を作成し、その辞書を という配列に追加しますmiArray

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

「id」という名前のキーを指定していないことに注意してください。

後で、 から辞書を取得しますmiArray。このディクショナリは、「username」などの 1 つのキーのみを使用して作成したディクショナリです。キー「id」のオブジェクトを取得するように指示しますが、そのキーを指定していないため、nil値を取得します。

要するに、私の解決策を試してください。

于 2012-05-21T01:39:10.633 に答える