【问题标题】:How to show json data in table view in xcode如何在xcode的表格视图中显示json数据
【发布时间】:2013-08-02 14:24:28
【问题描述】:

我是 ios 新手,目前正在使用 json。我只是使用在表格视图中显示的 iTunes 前 10 专辑我收到了我在表格视图中格式化和显示的数据,但我只能显示专辑名称,但我想显示要显示的特定专辑的所有详细信息同一个单元格。

这是我的完整代码

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

我怎样才能做到这一点帮助我..提前谢谢

【问题讨论】:

  • 研究文档。
  • (提示:首先将所有数据合并到一个 NSMutableArray 中,例如每行的 NSDictionaries。然后您只需将该数组以某种方式与表视图委托类进行通信。)
  • (请注意,您从 JSON 解析中返回的数组非常接近我上面提到的数组,如果不是完全一样的话。该数组是一切的核心。)(换句话说,不要'不要使用albumTop,使用total,只要它包含您需要的所有内容并且条目计数等是正确的。在cellForRowAtIndexPath内部引用total提取特定行所需的所有信息。)
  • 你能说得更详细一些吗,想想我有这两个值,比如 NSDictionary *albumname =[top objectForKey:@"im:artist" ]; NSString 标题 =[专辑名 objectForKey:@"label"]; NSDictionary *price =[top objectForKey:@"im:itemCount" ]; NSString track =[price objectForKey:@"label"];我现在同时拥有专辑名称和曲目编号,我如何将两者保存在同一个索引中以在表中检索
  • 将一张专辑的所有内容(我猜主要是一堆 NSStrings)放入 NSMutableDictionary,然后将字典放入 NSMutableArray 中,每个专辑都有一个条目。如果您有曲目列表,请为此创建一个数组并将其放入专辑的字典中。这是一个非常简单的“树结构”,只有一个“根”。如果您不知道如何创建 NSMutableDictionary 或将某些内容放入其中,请阅读文档——它既可以在线访问,也可以通过 Xcode 访问。

标签: ios json uitableview


【解决方案1】:

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

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

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

  return 80;// write as you need.
}

【讨论】:

  • 实际上我将专辑名称单独存储在数组中并将其传递给索引处的对象我如何将所有详细信息存储在数组中
  • @user2515863- 你的 JSON 数据是什么样子的 .. 我想展示它:)
  • 你能用那个url看看json数据是什么样子的吗
  • 好的,那么你的数组只包含像 (Jones, carry Kim) 这样的名字??
  • 没有数组将包含所有详细信息,如曲目数量、价格等,总共有 10 张专辑,但我显示的是虚拟数据
【解决方案2】:

你必须使用服装 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;

}

【讨论】:

  • 我知道必须使用自定义单元格,但是如何在表格的第一行中加载一个完整的专辑数据
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多