我已經閱讀過類似文章Reusable cell old image showing,我仍然遇到同樣的問題。從本質上講,我有一個TableView從亞馬遜S3下載圖像,當你向下滾動時,一切正常,直到你看到第12或第13張圖像。會發生什麼情況是,新圖像正在被下載時,該行中的圖像在新行中出現約2秒。這是我的代碼(我還是新手,學習IOS)。 stream_image_string作爲完整的URL下載圖像和PicHeight是一個整數與圖像高度保存,因爲每個圖像通常有不同的高度。IOS tableview在下載新映像時重新使用舊映像問題
var Stream_Cache = NSCache<AnyObject, AnyObject>()
var stream_image_string = [String]()
var PicHeight = [Int]()
這下面是在UITableViewCell中,首先我檢查是否有一個url將包含超過0個字符。然後我檢查圖像/ URL是否保存在緩存中,如果沒有,我下載它。
if stream_image_string[indexPath.row].characters.count > 0 {
if let image = Stream_Cache.object(forKey: stream_image_string[indexPath.row] as AnyObject) as? UIImage {
DispatchQueue.main.async(execute: {() -> Void in
cell.stream_image.image = image
})
} else {
if cell.stream_image != nil {
let strCellImageURL = self.stream_image_string[indexPath.row]
let imgURL: NSURL = NSURL(string: strCellImageURL)!
let request:NSURLRequest = NSURLRequest(url: imgURL as URL)
let config = URLSessionConfiguration.default
let session = URLSession(configuration: config)
cell.Stream_Image_Height.constant = CGFloat(Float(cell.pic_height!))
let task = session.dataTask(with: request as URLRequest, completionHandler: {(data, response, error) in
DispatchQueue.main.async(execute: {() -> Void in
if data != nil {
cell.stream_image.image = UIImage(data: data!)
} else {
cell.Stream_Image_Height.constant = 0
cell.stream_image.image = nil
}
})
});
task.resume()
}
}
} else {
cell.Stream_Image_Height.constant = 0
}
在我的UITableViewCell文件I圖像設置的情況下,它沒有這樣做加載新圖像的默認圖像,但並未奏效
class HomePageTVC: UITableViewCell {
@IBOutlet weak var stream_image: UIImageView!
var pic_height: Int?
override func awakeFromNib() {
super.awakeFromNib()
stream_image.image = #imageLiteral(resourceName: "defaultImage")
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
任何建議將是巨大
完美謝謝,這是我需要的 – user1591668