【发布时间】:2014-12-09 18:00:35
【问题描述】:
我正在编写显示 TableView 的应用程序,其中包含包含图像的条目。
我正在尝试通过在cellForRowAtIndexPath 方法中执行这行代码来获取图像:
cell.detailTextLabel.text = [artistData objectForKey:generesKey];
dispatch_async(backgroundQueue, ^{
NSURL *url_img = [NSURL URLWithString:[artistData objectForKey:pictureKey]];
NSData* data = [NSData dataWithContentsOfURL:
url_img];
cell.imageView.image = [UIImage imageWithData:data];
[self performSelectorOnMainThread:@selector(refreshCell:) withObject:cell waitUntilDone:YES];
});
设置图像后,我执行包含以下内容的选择器:
-(void)refreshCell:(UITableViewCell*)cell{
[cell setNeedsDisplay];
[self.view setNeedsDisplay];
[self.tableViewOutlet setNeedsDisplay];
}
并且没有显示图像,但是当我单击单元格或滚动整个列表时,会显示图像。为什么我的视图不刷新?我错过了什么吗?
【问题讨论】:
-
使用一些框架来下载异步图像会容易得多。比如 SDWebImage github.com/rs/SDWebImage 然后你只需要写
[cell.imageView sd_setImageWithURL:[NSURL urlWithString:@"http://somelink.png"]]
标签: ios objective-c uitableview setneedsdisplay