0
VC1にUITableview
を実装しました。セルにいくつかのJSONデータを表示します。 Modelクラスを実装して、データをテーブルビューに渡しました。テーブルビューのデータがモデルクラスを通過する
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]]];
どうしてですか?どうしたらいいですか?