我正在學習iOS編程this wonderful tutorial,除了我瞄準iOS 9並做了一些小的修改。iOS的UIImageView不會自動刷新
在下面的tableView()函數中,我可以下載縮略圖並調用處理程序,如從控制檯打印出兩條記錄行中可以看到的那樣。但是,當應用程序運行時(在模擬器中),我必須單擊每個表格單元才能顯示圖像。我試圖看看是否有refresh()
或類似於UIImageView
或表格單元中的內容,但什麼也沒找到。
如何在收到數據時立即顯示圖像?
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
}
哇,謝謝。 – yeyo
這不應該被接受的答案 – Rob