1
我已经创建了一个UITableView,并拥有一组需要设置其图像属性的URL。我设法通过使用dispatch_async检索图像来提高表格的滚动功能,并将它们存储在我声明为强属性的NSCache中,但现在当我滚动表格时,可以看到来自在出现的单元格中的前一行,然后当滚动停止时,图像更新为他们应该是,但有时他们也会循环通过一些其他图像。这是我在cellForRowAtIndexPathMethod中的代码,有谁知道为什么会发生这种情况?如何在桌面视图中滚动时停止重新加载图像
UIImage* image = [_imageCache objectForKey:article.imageURL];
if(image){
[cell.imageRight setImage:image];
} else {
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0ul);
dispatch_async(queue, ^{
UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:article.imageURL]]];
dispatch_async(dispatch_get_main_queue(), ^{
[_imageCache setObject:image forKey:article.imageURL];
[cell.imageRight setImage:img];
[cell setNeedsLayout];
});
});
}