0
我在VC1中實現了UITableview
,我想在單元格中顯示一些JSON數據。我已經實現了一個Model類來將數據傳遞給表視圖。Tableview數據通過模型類
dispatch_async(dispatch_get_main_queue(), ^{
NSURLSession*session=[NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithURL:[NSURL URLWithString:@"https://itunes.apple.com/search?term=music"] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSLog(@"%@", json);
NSArray *entryarr = [json objectForKey:@"results"];
TableClass *tc = [[TableClass alloc] init];
for (NSDictionary *appDict in entryarr) {
//setting title
NSString *str = [appDict objectForKey:@"artistName"];
[tc setTittle:str];
NSLog(@"artist Name=%@",tc.tittle);
//setting Subtitle
NSString *sub = [appDict objectForKey:@"country"];
[tc setSubtittle:sub];
NSLog(@"artist Name=%@",tc.subtittle);
//image
NSString *imageStr = [appDict objectForKey:@"artworkUrl60"];
NSURL *imageURL = [NSURL URLWithString:imageStr];
[tc setImage:imageStr];
NSData *imageData =[[NSData alloc] initWithContentsOfURL:imageURL];
//[self.imageArray addObject:imageData];
[_tableArray addObject:tc];
NSLog(@"%@ name of tittle",[_tableArray objectAtIndex:0]);
}
NSLog(@"%lu %lu %lu",(unsigned long)self.tableArray.count,(unsigned long)self.tableArray.count,(unsigned long)self.tableArray.count);
[self.tableView reloadData];
}];
[dataTask resume];
});
但是,當它訪問到單元格時,我得到了數組中的最後一個元素。
TableClass *tableclassModel = [self.tableArray objectAtIndex:indexPath.row];
cell.textLabel.text = tableclassModel.tittle;
cell.detailTextLabel.text = tableclassModel.subtittle;
cell.imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:tableclassModel.image]]];
爲什麼如此......?我該怎麼做?