我正在学习swift/ios的过程中,并且在将imageview设置为dispatch_async(dispatch_get_main_que)
的闭合中时,将图像显示在表视图单元格中时遇到了问题。p>这是我用于返回TableView的单元格的代码。请注意,调度呼叫已评论。在这种状态下,一切都可以。(图像显示出任何互动)
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: UITableViewCell
if indexPath.section == 3 {
cell = tableView.dequeueReusableCellWithIdentifier("imageCell", forIndexPath: indexPath) as! MentionedImageTableViewCell
if let images = tweetSections?[3].items as? [MediaItem] {
let imageUrl = images[indexPath.row].url
let qos = Int(QOS_CLASS_USER_INTERACTIVE.value)
let queue = dispatch_get_global_queue(qos, 0)
println("feching image for cell = (cell)")
// dispatch_async(queue) {
if let imageData = NSData(contentsOfURL: imageUrl), let image = UIImage(data: imageData) {
println("fetch complete")
// dispatch_async(dispatch_get_main_queue()) {
println("posting image = (image), to cell = (cell)")
cell.imageView?.image = image
println("cells image = (cell.imageView?.image)")
// }
}
// }
}
} else {
cell = tableView.dequeueReusableCellWithIdentifier("textCell", forIndexPath: indexPath) as! UITableViewCell
if let keywords = tweetSections![indexPath.section].items as? [Tweet.IndexedKeyword] {
let text = keywords[indexPath.row].keyword
cell.textLabel?.text = text
}
}
return cell
}
但是,当我将更新包裹在传递给dispatch_async(get_main_que(){}的闭合中时,直到我单击它或旋转设备后才出现图像请注意,该代码和上面的唯一区别是未被选中的两行。笔记;所有这些都在模拟器上
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: UITableViewCell
if indexPath.section == 3 {
cell = tableView.dequeueReusableCellWithIdentifier("imageCell", forIndexPath: indexPath) as! MentionedImageTableViewCell
if let images = tweetSections?[3].items as? [MediaItem] {
let imageUrl = images[indexPath.row].url
let qos = Int(QOS_CLASS_USER_INTERACTIVE.value)
let queue = dispatch_get_global_queue(qos, 0)
println("feching image for cell = (cell)")
// dispatch_async(queue) {
if let imageData = NSData(contentsOfURL: imageUrl), let image = UIImage(data: imageData) {
println("fetch complete")
dispatch_async(dispatch_get_main_queue()) {
println("posting image = (image), to cell = (cell)")
cell.imageView?.image = image
println("cells image = (cell.imageView?.image)")
}
}
// }
}
} else {
cell = tableView.dequeueReusableCellWithIdentifier("textCell", forIndexPath: indexPath) as! UITableViewCell
if let keywords = tweetSections![indexPath.section].items as? [Tweet.IndexedKeyword] {
let text = keywords[indexPath.row].keyword
cell.textLabel?.text = text
}
}
return cell
}
如果您必须以某种方式将图像分配放入GCD(可能是用于异步图像加载),则必须调用以下方法,第二个方法是可选的,取决于您对更新的需求频率:
cell.setNeedsLayout() //invalidate current layout
cell.layoutIfNeeded() //update immediately
我猜| tableview:cellforrowatIndExpath:|隐含地调用上述方法。因此,在大多数情况下,您不必关心这一点。但是,如果您不同步,上述数据源方法调用这些方法的时间,根本没有图像可显示。因此,有时您会得到图像,您必须在图像分配后明确称其为正确的视图更新。
进行了一些测试后,数据源方法简单地调用两个方法| setNeedsdisplay |只有一次之前未打电话,然后| setneedslayout |几次
对我来说,我在textlabel下找到它,您需要做的一切:
cell.textLabel?.backgroundColor = .clear