如何在xcode的表视图中显示json数据

问题描述 投票:1回答:2

我是ios的新手,目前正在json工作。我只是使用itunes十大专辑,在桌面视图中显示我收到的数据我格式化并显示在表视图中,但我只能显示专辑名称,但我想显示特定专辑的所有细节,将显示在相同的细胞。

这是我的完整代码

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

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

我只能获取单个值,我怎么能显示这样的相册的所有细节

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

我怎么能实现这一点帮助我..提前谢谢

ios json uitableview
2个回答
0
投票

您需要在UITableViewCell中添加Thee标签。

并根据需要获取每个值。当你获取name的值并将它放到相关的UILabel时,它很简单。您还需要通过以下方法扩展UITableView单元格的大小。

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath
{

  return 80;// write as you need.
}

0
投票

你必须使用自定义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;

}

© www.soinside.com 2019 - 2024. All rights reserved.