如何在uitableview上显示json数据 [英] How to display json data on uitableview..
本文介绍了如何在uitableview上显示json数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
JSON数据就是这样..
JSON data is something like that..
{"order_list_1":[
{"name":"@SEVEN (7) OCLOCK BLADE PLATINUM","qty":1,"mrp":1.0},
{"name":"ACT 2 POPCORN BUTTER PEPPER","qty":2,"mrp":2.0},
{"name":"ACT 2 POPCORN CHILLY SURPRISE","qty":3,"mrp":3.0},
{"name":"@MAGGI SOUP HOT N SOUR(1 1)","qty":4,"mrp":4.0},
{"name":"ACT 2 POPCORN GOLDEN SIZZLE","qty":5,"mrp":5.0},
{"name":"AMCHUR AAKHA 1kg","qty":6,"mrp":6.0}]
}
jSon数据显示在控制台上,而不显示在表视图上.
这是我的代码片段...
jSon data is displaying on console but not on tableview..
Here my code snipest...
(void)fetchData{
dispatch_async(bKQueue, ^{
NSData *data = [NSData dataWithContentsOfURL:latestURL];
NSLog(@"LatestURL:%@",latestURL);
NSError* error=nil;
//Creating JSON Object
NSDictionary *jsonDict= [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSLog(@"JSON = %@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
//NSLog(@"dataaaa :%@",jsonDict);
dispatch_async(dispatch_get_main_queue(), ^{
//[self fetchData:responseData];
[self.tableView reloadData];
//NSLog(@"Value :%@",data);
});
NSDictionary *statuses=[jsonDict objectForKey:@"order_list_1"];
NSLog(@"SomeStatus :%@",statuses);
if (!statuses)
{
NSLog(@"Error in Json :%@",error);
}
else
{
for(NSDictionary *newValu in statuses)
{
NSString *name=[newValu objectForKey:@"name"];
NSString *qty=[newValu objectForKey:@"qty"];
NSString *mrp=[newValu objectForKey:@"mrp"];
NSLog(@"Name :%@ Quantity :%@ MRP :%@ ",name,qty,mrp);
}
}
});
}
And this is my Tableview code..
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSError *error=nil;
NSDictionary *jsonDict=[NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
NSArray *statuses=[jsonDict objectForKey:@"order_list_1"];
for (int i=0; i<jsonDict.count; i++) {
NSDictionary *newDict=[statuses objectAtIndex:i];
NSLog(@"name :%@",[newDict valueForKey:@"name"]);
}
NSDictionary *text=[statuses objectAtIndex:indexPath.row];
cell.textLabel.text=[text objectForKey:@"name"];
cell.textLabel.text=[text valueForKey:@"qty"];
cell.textLabel.text=[text valueForKey:@"mrp"];
[self.tableView reloadData];
return cell;
}
推荐答案
cellForRowAtIndexPath为表中的每一行调用.通过实现方法指定行数-
cellForRowAtIndexPath is called for each row in the table. And number of rows are specified by implementing the method -
(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
因此,在此方法中指定行数,然后调用[self.tableView reloadData];在退出fetchData方法之前,不在cellForRowInIndexPath中.希望对您有所帮助.
So specify number of rows in this method and call [self.tableView reloadData]; before exiting fetchData method, not in cellForRowInIndexPath. I hope this will help.
这篇关于如何在uitableview上显示json数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文