0
我有一张小图片的表格。它是如何工作的: 我有一个模型,它接受来自Instagram的JSON响应并将其全部放入字典数组中。 在我的方法cellForRowAtIndexPath我设置单元格的标记为indexpath.row,然后开始下载图像(从模型获取url)通过使用dispatch_async。 图像加载完成后,我有一个检查来比较当前单元格和当前indexpath.row的标记,如果它们相同,则绘制图像。 它工作正常,直到我刷新我的模型。简单地重新载入相同的数据会导致表格上出现奇怪的行为 - 它将前三个单元显示为相同的图像。 我该如何解决这个问题?更新UITableView - 错误的单元格和相同的图像
这里是我的细胞的方法:
- (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;
}
我试图把[实现代码如下reloadData]无处不在,但它并不能帮助。