【发布时间】:2015-11-05 05:51:16
【问题描述】:
我正在关注 this wonderful tutorial 学习 iOS 编程,但我的目标是 iOS 9 并进行一些小修改。
在下面的 tableView() 函数中,我可以下载缩略图并调用我的处理程序,这从控制台打印出的两条日志记录行可以看出。但是,当应用程序运行时(在模拟器中),我必须单击每个表格单元格才能显示图像。我试图查看UIImageView 或表格单元格中是否有refresh() 或类似的东西,但什么也没找到。
如何让图片在收到数据后立即显示?
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier(kCellIdentifier)!
let album = self.albums[indexPath.row]
// ... setting up other cell's data, see the tutorial link
// Start by setting the cell's image to a static file
// Without this, we will end up without an image view!
cell.imageView?.image = UIImage(named: "Blank52")
let request: NSURLRequest = NSURLRequest(URL: thumbnailURL)
let urlSession = NSURLSession.sharedSession()
urlSession.dataTaskWithRequest(request, completionHandler: {(data, response, error) -> Void in
print("received thumbnail \(thumbnailURLString)") // reached
if error == nil {
// Convert the downloaded data in to a UIImage object
let image = UIImage(data: data!)
// Update the cell
dispatch_async(dispatch_get_main_queue(), {
print("...dispatched thumbnail image for \(thumbnailURLString)") // reached
if let cellToUpdate = tableView.cellForRowAtIndexPath(indexPath) {
cellToUpdate.imageView?.image = image
}
})
}
}).resume()
return cell
}
【问题讨论】:
标签: swift uiimageview uikit ios9