如何在XCode的表格视图中显示JSON数据 [英] How to show json data in table view in xcode

查看:131
本文介绍了如何在XCode的表格视图中显示JSON数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是ios的新手,目前正在json中工作.我只是使用itunes要在表格视图中显示的前10张专辑,我已接收数据,并在表格视图中进行了格式化和显示,但是我只能显示专辑名称,但我想显示要在其中显示的特定专辑的所有详细信息同一单元格.

i am new to ios and am currently working in json. I am just using itunes top 10 albums which to be displayed in table view i received data i formatted and displayed in table view but i can able to display only album name but i want to display all details of particular album which is to be displayed in same cell.

这是我的完整代码

- (void)viewDidLoad
{
    [super viewDidLoad];

    albumtop=[[NSMutableArray alloc]init];

    [[self itunestTable]setDataSource:self];
    [[self itunestTable]setDelegate:self];

    NSURL *url=[NSURL URLWithString:@"https://itunes.apple.com/in/rss/topalbums/limit=10/json"];
    NSURLRequest *req=[NSURLRequest requestWithURL:url];

    connection=[NSURLConnection connectionWithRequest:req delegate:self];
    if(connection)
    {
        albumdata =[[NSMutableData alloc]init];
    }
}

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    [albumdata setLength:0];
}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [albumdata appendData:data];
}

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@"error in connection");
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSDictionary *alldata =[NSJSONSerialization JSONObjectWithData:albumdata options:0 error:nil];
    NSDictionary *album =[alldata objectForKey:@"feed"];
    NSArray *total =[album objectForKey:@"entry"];

    for(NSDictionary *top in total)
    {
        NSDictionary *albumname =[top objectForKey:@"im:artist" ];
        title =[albumname objectForKey:@"label"];
        [albumtop addObject:title];
    }
    [[self itunestTable]reloadData];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [albumtop count];
}

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

    if(!cell)
    {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellidentifier];
    }

    cell.textLabel.text=[albumtop objectAtIndex:indexPath.row];
    return cell;
}

程序运行正常

我的输出将是这样

 ---------------------------------
      Jones

---------------------------------------
     carry Kim

---------------------------------------

我只能获取单个值,这样才能显示相册的所有详细信息

i can able to fetch single value only how can i display all the details of the album like this

----------------------------------
  Jones
  no.tracks 10
  price 180
---------------------------------------
  carry Kim
  no.tracks 10
  price 180
---------------------------------------

我该如何实现这一点对我有所帮助..在此先感谢

how i can achieve this help me.. thanks in advance

推荐答案

您必须使用Costum UITableviewCells

you have to use costum UITableviewCells

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BDCustomCell"];
if (cell == nil) {
    // Load the top-level objects from the custom cell XIB.
    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"BDCustomCell" owner:self options:nil];
    // Grab a pointer to the first object (presumably the custom cell, as that's all the XIB should contain).
    cell = [topLevelObjects objectAtIndex:0];
}

return cell;

}

这篇关于如何在XCode的表格视图中显示JSON数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆