我在UITableviewcell
中遇到問題當滾動時圖像不斷變化,並且有時候將錯誤的圖像設置爲單元格!
當我滾動時桌面單元格中的圖像發生變化
let request = NSURLRequest(URL: url)
cell.img.contentMode = .ScaleAspectFit
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) {
(response: NSURLResponse?, data: NSData?, error: NSError?) -> Void in
if(error != nil){
print(error)
}
if let imageData = data as NSData? , let image = UIImage(data: imageData) where imageData.length != 0 {
cell.img.image = image
cell.img.contentMode = contentMode
return;
}
let cachedResponse = NSURLCache.sharedURLCache().cachedResponseForRequest(request)
if let cachedResponse = cachedResponse , let image = UIImage(data: (cachedResponse.data)){
cell.img.image = image;
cell.img.contentMode = contentMode
}
}
GIF顯示的情況:
http://im2.ezgif.com/tmp/ezgif-2265120982.gif
注:
我在計算器搜索,我發現的問題和答案,但對於沒有工作我的其中一個問題:
Wrong image is set in my tableview cell sometimes when I scroll on my tableview. How can I fix this?
檢查[此信息庫(https://github.com/pkc456/AbstractDownloading)更快,平滑滾動同時在tableview中下載圖像。 – pkc456
它改變因爲它重用....你需要應用邏輯使其相同..它將是你自己的.. – JAck
使用'SDWebImage'庫下載圖像,而不是'NSURLConnection',因爲這個庫是用來處理這個'async'任務。 –