【发布时间】:2016-07-05 07:55:59
【问题描述】:
我在ViewController 中实现了UITableview。我想在通过JSON 获取的TableView 中显示一些数据。
- (void)getData
{
NSURLSession*session=[NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithURL:[NSURL URLWithString:@"https://itunes.apple.com/us/rss/topaudiobooks/limit=10/json"] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSLog(@"%@", json);
NSArray*entryarr=[[json objectForKey:@"feed"]objectForKey:@"entry"];
for (NSDictionary*appDict in entryarr) {
NSString*title=[[appDict objectForKey:@"im:name"]objectForKey:@"label"];
NSString*imageStr=[[[appDict objectForKey:@"im:image"]objectAtIndex:2]objectForKey:@"label"];
NSURL*imageURL=[NSURL URLWithString:imageStr];
NSData*imageData=[[NSData alloc]initWithContentsOfURL:imageURL];
[self.imageArray addObject:imageData];
[self.tittleArray addObject:title];
[self.termArray addObject:title];
}
NSLog(@"%lu %lu %lu",(unsigned long)self.imageArray.count,(unsigned long)self.tittleArray.count,(unsigned long)self.termArray.count);
}];
[dataTask resume];
}
现在,当我将数组分配给 TableView 单元格时,它给出的数组是空的。 但在 getData 方法中,Array 由 10 个元素组成。
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return _tittleArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString*reUse=@"use";
UITableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:reUse];
if (cell==nil) {
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reUse];
cell.textLabel.textColor = [UIColor orangeColor];
}
cell.textLabel.text=[self.tittleArray objectAtIndex:indexPath.row];
cell.imageView.image=[self.imageArray objectAtIndex:indexPath.row];
return cell;
}
为什么会这样。?我该怎么做。?谁能给我有效的解决方案?
【问题讨论】:
-
reloadData is missing ... .reload your tableview
-
不是三个单独的数组使用一个数组和一个包含三个属性的自定义类作为数据模型。这是面向对象的编程。
-
结帐给出答案。
标签: ios objective-c json uitableview