我正在尝试tableView:cellForRowAtIndexPath
通过调用来设置图像
[self downloadImage:urlString andSetIntoCellImageView:cell]
在 中downloadImage
,它将调用NSURLConnection:sendAsynchronousRequest
(仅限 iOS 5 及更高版本),并在完成块中,使用
cell.imageView.image = [UIImage imageWithData:data]; // data is downloaded data
如果 in 中填充了一个虚拟占位符图像,它就可以工作tableView:cellForRowAtIndexPath
——imageView
我想知道新图像是如何刷新的,是不是setNeedsDisplay
要重新绘制?但是如果我不设置占位符图像,那么新图像根本不会显示。我想知道可以使用什么机制使其显示图像?
如果我使用
[cell.imageView setNeedsDisplay]
或者
[cell setNeedsDisplay];
在完成块中,它不起作用,如果我使用
[self.table reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationAutomatic];
在完成块中通过downloadImage
接受indexPath
,它会tableView:cellForRowAtIndexPath
再次调用,并导致无限循环。所以似乎我需要使用一些哈希表来记住图像是否已经在哈希表中:如果没有,则调用downloadImage
,如果在哈希表中,只需使用它,这样就不会有无限循环。
但是有没有一种简单的方法可以使图像显示出来?设置占位符图像有效,但如果我们不这样做 - 占位符通过什么机制导致图像刷新?