1

Web サービスからの応答である次の JSON データがあります。

     {"d": [{"raumKlassenObject":"Telepresenzraum"},
{"raumKlassenObject":"Besprechungsraum"},
{"raumKlassenObject":"Videokonferenzraum"},
{"raumKlassenObject":"IT-Schulungsraum"},
{"raumKlassenObject":"Konferenzraum"}]}

Data Telepresenzraum、Besprechungsraum などを TableView に表示するにはどうすればよいですか。

これまでのところ、私のコードはここにあります。

    - (void)viewDidLoad
{
    [super viewDidLoad];

    dic = [NSJSONSerialization JSONObjectWithData:result options:kNilOptions error:nil];
    array = [dic allKeys];

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    // Return the number of rows in the section.
    return array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

   cell.textLabel.text=[NSString stringWithFormat:@"%@",[dic objectForKey:@"d"]];    



return cell;
}

私がそれを好きなら、Tableviewの出力は

 [{"raumKlassenObject":"Telepresenzraum"},{"raumKlassenObject":"Besprechungsraum"},{"raumKlassenObject":"Videokonferenzraum"},{"raumKlassenObject":"IT-Schulungsraum"},{"raumKlassenObject":"Konferenzraum"}]

Telepresenzraum、Besprechungsraum、Videokonferenzraum などのみをテーブルビューの行に表示する方法がわかりません。

助けてくれてありがとう。

4

2 に答える 2

2

トップレベルの辞書にはキー"d"が1つしかないため、array.countは1になり、[dic objectForKey:@"d"]は辞書の配列全体になります。

したがって、この再定義配列を修正するには、次のようにします。

array = [[dic valueForKey:@"d"] valueForKey:@"raumKlassenObject"];

これにより、そのキーのすべての値の配列が得られます。次に、以下の最初の行を2番目の行に置き換えます。

cell.textLabel.text= [NSString stringWithFormat:@"%@",[dic objectForKey:@"d"]]; 
cell.textLabel.text= [array objectAtIndex:indexPath.row];
于 2012-06-04T04:50:05.860 に答える
0

の nil-check が必要ですcell。少しスクロールするまではゼロですnil の場合は、UITableView セル クラスの init メソッドを使用して実際にセルを作成する必要があります。

于 2012-06-04T02:40:07.947 に答える