nPn*_*nPn 2 uitableview uiimageview ios swift
我正在学习swift / IOS,并且在将imageView设置为传递给的闭包内时,无法在表视图单元格中显示图像时遇到问题dispatch_async(dispatch_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
}
Run Code Online (Sandbox Code Playgroud)
但是,当我将更新包装在传递给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
}
Run Code Online (Sandbox Code Playgroud)
如果必须以某种方式将图像分配放入GCD(可能用于异步图像加载),则必须调用以下方法,第二种方法是可选的,具体取决于您对更新频率的要求:
cell.setNeedsLayout() //invalidate current layout
cell.layoutIfNeeded() //update immediately
Run Code Online (Sandbox Code Playgroud)
我猜想| tableView:cellForRowAtIndexPath:| 隐式调用上述方法。因此,在大多数情况下,您不必担心。但是,如果以异步方式进行操作,则在上述数据源方法调用这些方法时,根本没有图像可以显示。因此有时在获取图像后,必须在分配图像后显式调用它以获取正确的视图更新。
经过测试后,数据源方法仅调用两个方法| setNeedsDisplay |。如果之前未调用过| setNeedsLayout |仅一次 几次