我正在创建一个自定义UITableViewCell
以包含一个UIImageView
和一些相关文本。我从 Internet 获取图像,当图像加载时,我会显示一个临时的UIImage
.
我正在使用单独的线程来获取UIImage
. 一旦UIImage
下载了一个,我就会在主线程上触发一个方法来设置图像UIImageView
(获取UIImageView
使用标签的实例)
但无论我做什么,图像都不会改变。单元格仍显示较旧的图像(对应于不同的行 - 由于单元格的重复使用)并且不会反映新图像。日志语句显示正在调用设置图像的方法。
我什至尝试将图像更改为静态图像,willDisplayCell:forRowAtIndexPath:
但即使那样图像也不会改变。
编辑
我尝试在更改后调用reloadData
,UITableView
但UIImage
仍然UIImage
没有更改。
这是相关的代码
cellForRowAtIndexPath
image = [imagesArray valueForKey:[NSString stringWithFormat:@"%i",indexPath.row]];
if(image == nil){
//display temporary images. When scrolling stops, detach new thread to get images for rows visible
NSString *pth = [[NSBundle mainBundle] pathForResource:@"folder" ofType:@"png"];
image = [UIImage imageWithContentsOfFile:pth];
[imgView setImage:image];
}
else{
[imgView setImage:image];
}
下载图片后在主线程调用的方法
-(void)setImageInArray:(NSIndexPath *)indPath{
[filesTable reloadData];
}
在线程中,我将UIImage
对象添加到imagesArray
. 因此,当下次cellForRowAtIndex
调用该方法时reloadData
,UIImage
应该显示新的。