2012-06-04 98 views
1

我有以下JSON数据,这是来自我的Web服务的响应。如何在UITableView中显示JSON数据

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

如何在TableView中显示数据Telepresenzraum,Besprechungsraum等...。

继承人我的代码到目前为止。

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

如果我这样做,在泰伯维输出

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

我不知道如何在tableview中的行只显示Telepresenzraum,Besprechungsraum,Videokonferenzraum等。

Thx寻求帮助。

回答

2

你顶级字典只有一个键“d”,所以array.count将是1,[dic objectForKey:@“d”]将是整个字典数组。

因此,要解决这个问题重新定义数组:

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

这应该给你的那个键中的所有值的数组。然后,代替第一线下方与第二:

cell.textLabel.text= [NSString stringWithFormat:@"%@",[dic objectForKey:@"d"]]; 
cell.textLabel.text= [array objectAtIndex:indexPath.row]; 
+0

thx它的工作。多谢 – Bashud

0

你需要一个零检查cell。它为零,直到你滚动了一下。如果它为零,则需要使用UITableView单元类init方法实际创建单元。

+0

我没有,现在,但那不是固定我的问题/疑问 – Bashud

+0

'cell.textLabel.text = [DIC objectForKey:[阵列objectAtIndex:indexPath.row] ];' – borrrden

+0

其输出仍然相同[{“raumKlassenObject”:“Telepresenzraum”},{“raumKlassenObject”:“Besprechungsraum”},{“raumKlassenObject”:“Videokonferenzraum”},{“raumKlassenObject”:“IT-Schulungsraum” },{ “raumKlassenObject”: “Konferenzraum”}]。 – Bashud