テキストの少ない画像の表があります。仕組み: Instagram から JSON 応答を受け取り、それをすべて辞書の配列に入れるモデルがあります。メソッド cellForRowAtIndexPath で、セルのタグを indexpath.row に設定し、dispatch_async を使用して画像のダウンロード (モデルから URL を取得) を開始します。画像の読み込みが完了したら、現在のセルと現在の indexpath.row のタグを比較して、同じであれば画像を描画します。モデルを更新するまでは問題なく動作します。同じデータをリロードするだけで、テーブルで奇妙な動作が発生します。最初の 3 つのセルが同じ画像として表示されます。どうすれば修正できますか?
これが私のセルメソッドです:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.tag = indexPath.row;
if (self.loader.parsedData[indexPath.row] != nil)
{
cell.imageView.image = nil;
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^(void) {
NSString *url = [self.loader.parsedData[indexPath.row] objectForKey:@"imageLR"];
if ([self.cache objectForKey:url] != nil)
{
NSData *imageData = [self.cache objectForKey:url];
self.tempImage = [[UIImage alloc] initWithData:imageData];
}
else
{
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]];
self.tempImage = [[UIImage alloc] initWithData:imageData];
[self.cache setObject:imageData forKey:[self.loader.parsedData[indexPath.row] objectForKey:@"imageLR"]];
}
dispatch_async(dispatch_get_main_queue(), ^{
if (cell.tag == indexPath.row)
{
cell.imageView.image = self.tempImage;
[cell setNeedsLayout];
}
});
});
cell.textLabel.text = [self.loader.parsedData[indexPath.row] objectForKey:@"id"];
}
return cell;
}
[tableview reloadData] をどこにでも置いてみましたが、役に立ちません。