2013-08-02 110 views
1

我是ios的新手,目前在json工作。我只是使用iTunes的十大专辑,这将显示在表格视图我收到的数据我格式化和显示在表视图,但我可以只显示专辑名称,但我想显示特定专辑的所有细节将显示在相同的细胞。如何在xcode中的表格视图中显示json数据

这里是我完整的代码

- (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 *[email protected]"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 
--------------------------------------- 

我怎么能达到这个帮助我..提前致谢

+1

研究。 –

+0

(提示:首先将所有数据组合到一个NSMutableArray中,例如每行有NSDictionaries,然后你只需要以某种方式将该数组传递给表视图委托类) –

+0

(并注意你从数组中返回的数组JSON解析与上面提到的数组非常接近,如果不是完全一样的话,那么这个数组就是一切的核心。)(换句话说,不要使用'albumTop',只要它包含所有东西你需要和入口计数等是正确的。在cellForRowAtIndexPath引用'total'里提取你需要的特定行的所有信息。) –

回答

0

你需要在你的UITableViewCell中添加Thee标签。

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

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

    return 80;// write as you need. 
} 
+0

其实我只是在数组中存储专辑名称并将它传递给索引中的对象我怎样才能存储数组中的所有细节 –

+0

@ user2515863-您的JSON数据的外观如何..我想展示它:) – iPatel

+0

您可以使用那个看看如何JSON数据看起来像 –

0

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

}文档

+0

我知道必须使用自定义单元格,但我如何在表格一行中加载一个完整的专辑数据 –

相关问题