【发布时间】: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