这就是我创建细胞的方式:
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内部的事实密切相关!
答案 0 :(得分:6)
确保更新主队列上的图像
dispatch_async(dispatch_get_main_queue(), { () -> Void in
//Update UI
})
之前我有这个问题,这解决了我的问题。只需尝试
答案 1 :(得分:2)
我认为您异步下载图像。当程序到达println("图像已设置!")行时,您的日志会出现,但实际上您的图像尚未下载。您应该将日志移动到下载回调的末尾,然后在出现图像时及时显示日志。
或者这可能发生,因为您没有在UI线程中更新UIImageView。你可以这样做:
dispatch_async(dispatch_get_main_queue(), { () -> Void in
//Set image to UIImageView
})
答案 2 :(得分:1)
有一种更好的方法可以在Swift3中访问UI线程。
DispatchQueue.main.async() {
}
有人说这比dispatch_async
慢,但我从未见过实际使用上的任何差异。