【问题标题】:How to refresh an UIImage in an UITableViewCell?如何刷新 UITableViewCell 中的 UIImage?
【发布时间】:2015-06-05 13:48:43
【问题描述】:

这就是我创建单元格的方式:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCellWithIdentifier("FbFriendCell") as! FbFriendCell
        cell.initialize()
        return cell  
}

很简单。

在 initialize() 函数中,我下载了一张图片并将其放入 UIImageView 中:

let theImageView = UIImageView(image: userPhoto)
theImageView.frame = CGRect(x: 0, y: 0, width: 80, height: 80)
self.addSubview(theImageView)
theImageView.image = userPhoto
println("The image has been set !")

问题是图像仅在此日志消息后几秒钟出现:

println("The image has been set !")

如何强制重新加载图像?

* 阅读本文 * => 该问题可能与图像位于 UITableViewCell 内的事实密切相关!

【问题讨论】:

  • 发布您用于下载图像的代码。听起来这需要在完成块中处理。
  • 在完成块中处理。

标签: ios objective-c uitableview swift uiimageview


【解决方案1】:

确保更新主队列中的图像

 dispatch_async(dispatch_get_main_queue(), { () -> Void in
            //Update UI
 })

我以前也有这个问题,这解决了我的问题。试试吧

【讨论】:

    【解决方案2】:

    我认为您是异步下载图像的。当程序到达 println("The image has been set !") 行时会显示您的日志,但实际上您的图像尚未下载。您应该将日志移动到下载回调的末尾,然后日志应该会在图像出现时及时出现。

    另外,这也可能发生,因为您没有在 UI 线程中更新 UIImageView。你可以这样做:

    dispatch_async(dispatch_get_main_queue(), { () -> Void in
            //Set image to UIImageView
    })
    

    【讨论】:

    • 图像会在几秒钟后显示。如果我是同步设置的话,根本就不会出现。
    【解决方案3】:

    在 Swift3 中有一种更好的方式来访问 UI 线程。

    DispatchQueue.main.async() {
    
    }
    

    有人说这比dispatch_async慢,但是我在实际使用中没看到有什么区别。

    【讨论】:

      猜你喜欢
      • 2010-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多