我正在编写一个应用程序来显示带有包含图像的条目的 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];
}
并且未显示图像,但是当我单击单元格或滚动整个列表时,会显示图像。为什么我的 View 不刷新?我错过了什么吗?
最佳答案
在单元格上调用 setNeedsLayout()
就足够了。
在 swift 4 中它看起来像这样:
DispatchQueue.global().async {
let data = try? Data(contentsOf: URL(string: imageUrl)!)
DispatchQueue.main.async {
cell.imageView?.image = UIImage(data: data!)
cell.setNeedsLayout()
}
}
关于ios - 通过 dispatch_async 获取图像数据后刷新 UITableViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27385656/