ios - 通过 dispatch_async 获取图像数据后刷新 UITableViewCell

标签 ios objective-c uitableview setneedsdisplay

我正在编写一个应用程序来显示带有包含图像的条目的 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/

相关文章:

objective-c - iOS实时应用

objective-c - 将图像发送到另一个 View Controller UIImageView IOS 5.1

swift - 我想使用 swift 创建一个自动滚动的 tableview

ios - 如何在Azure DevOps中安装net6的iOS工作负载?

ios - 如何将对象放置在#define语句中

objective-c - 如何搜索目录 ios

ios - 将 tableview 导出作为 cellForRowAtindexPath 中的参数传递

iphone - 我的应用程序文本的配置文件

android - 无法将现有的 FaceBook 页面链接到新创建的 FaceBook 应用程序

ios - 使用 cocoapods 时 Realm.io 构建错误 `use_frameworks!`