我使用NSJSONSerialization类解析json并能够在控制台日志中显示所有数据。但不知道如何将所有数据显示到表视图中。以下是我的代码。请帮我看看如何在tableview中显示国家名称和标志图像。在此先感谢。
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
/* NSLog(@"%d",webdata.length);
NSString *receivedstring=[[NSString alloc]initWithData:webdata encoding:NSASCIIStringEncoding];
NSLog(@"%@",receivedstring);
*/
NSError *e = nil;
NSMutableDictionary *jsonArray = [NSJSONSerialization JSONObjectWithData: webdata options: NSJSONReadingMutableContainers error: &e];
feedforentry=[jsonArray objectForKey:@"worldpopulation"];
// NSLog(@"%d",feedforentry.count);
for (NSDictionary *review in feedforentry) {
name = [review objectForKey:@"country"];
NSLog(@"%@",name);
NSString *populations = [review objectForKey:@"population"];
NSLog(@"%@",populations);
NSString *rank = [review objectForKey:@"rank"];
NSLog(@"%@",rank);
NSString *flag = [review objectForKey:@"flag"];
NSLog(@"%@",flag);
}
[self.tableview reloadData];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [feedforentry count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if(!cell)
{
cell = [[UITableViewCell alloc] init];
}
//cell.textLabel.text=[[feedforentry objectAtIndex:indexPath.row]nam ];
return cell;
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
@end
我想在tableview中显示国家/地区名称和标记图像。提前谢谢。
答案 0 :(得分:1)
试试这个
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if(!cell)
{
cell = [[UITableViewCell alloc] init];
}
NsDictionary *review = [feedforentry objectAtIndex:indexPath.row];
NSString *rank = [review objectForKey:@"rank"];
Cell.textlabel.text = rank;
Return cell;
}
答案 1 :(得分:0)
你拥有的代码是正确的想法。在tableView:cellForRowAtIndexPath:
方法中,您只需获取详细信息
NSDictionary *countryInfo = feedforentry[indexPath.row];
NSString *name = countryInfo[@"country"];
NSString *flag = countryInfo[@"flag"];
然后使用这些值来设置单元格。
答案 2 :(得分:0)
方法tableView:cellForRowAtIndexPath:
是你应该像这样配置你的单元格的地方:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if(!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
}
NSDictionary *dict = feedforentry[indexpath.row];
cell.textLabel.text= [NSString stringWithFormat:@"%@ - %@", dict[@"flag"], dict[@"country@"]];
return cell;
}
希望这有帮助。
答案 3 :(得分:0)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath*)indexPath
{
static NSSting *CellIdntifier=@"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdntifier];
if(!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdntifier];
}
NSDictionary *dict = feedforentry[indexpath.row];
cell.textLabel.text= dict[@"country"];
cell.imageView.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:dict[@"flag"]]]];
return cell;
}