我是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
---------------------------------------
我怎么能达到这个帮助我..提前致谢
研究。 –
(提示:首先将所有数据组合到一个NSMutableArray中,例如每行有NSDictionaries,然后你只需要以某种方式将该数组传递给表视图委托类) –
(并注意你从数组中返回的数组JSON解析与上面提到的数组非常接近,如果不是完全一样的话,那么这个数组就是一切的核心。)(换句话说,不要使用'albumTop',只要它包含所有东西你需要和入口计数等是正确的。在cellForRowAtIndexPath引用'total'里提取你需要的特定行的所有信息。) –