问题描述
我从上到下滚动时遇到问题,然后我的 tableview
可重复使用的单元格显示旧图像,直到新图像下载完成。
I am facing a problem when scrolling top to bottom then my tableview
reusable cell showing old image until new image download completed.
它应显示我的默认图像占位符,直到下载完成后下载新图像,然后将imageview从图像占位符更改为当前下载图像。我该怎么办?
It should show my default image placeholder until new image download when download finish then change the imageview from image placeholder to current download image. What should i do ?
抱歉英语不好。如果您需要任何信息,请告诉我们谢谢
Sorry for bad English. If you need any information let me know thanks
更新
TableViewcontroller:
TableViewcontroller :
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ItemTableViewCell") as! ItemTableViewCell
let itemInfo = itemInfos[indexPath.row]
var image1 : UIImage?
var image2 : UIImage?
if let imgUrl1 = itemInfo.imageUrl {
image1 = ItemListViewController.imageCache.object(forKey: imgUrl1 as AnyObject) as? UIImage
}
if let imgUrl2 = itemInfo.cookerProfilePicUrl{
image2 = ItemListViewController.imageCache.object(forKey: imgUrl2 as AnyObject) as? UIImage
}
cell.configureCell(iteminfo: itemInfo, img1 : image1 ,img2 : image2)
return cell
}
Xib:
func configureCell(iteminfo:ItemInfo , img1 : UIImage? , img2 : UIImage? ){
if img1 != nil {
imageViewItemPic.image = img1
}
else{
print("hi1")
imageViewItemPic.setImageFromURL(url: iteminfo.imageUrl!)
}
if img2 != nil {
imageViewCookerProfilePic.image = img2
}
else{
imageViewCookerProfilePic.setImageFromURL(url: iteminfo.cookerProfilePicUrl!)
}
labelItemHeading.text = iteminfo.heading
labelItemDescription.text = iteminfo.description
}
更新:
override func awakeFromNib() {
super.awakeFromNib()
self.imageViewItemPic.image = UIImage(named: "resto-placeholder.png")
}
更新:
extension UIImageView {
func setImageFromURL(url: String) {
DispatchQueue.global().async {
let data = NSData.init(contentsOf: NSURL.init(string: url) as! URL)
DispatchQueue.main.async {
let image = UIImage.init(data: data as! Data)
ItemListViewController.imageCache.setObject(image!, forKey: url as AnyObject)
self.image = image
}
}
}
}
推荐答案
由于它是一个可重复使用的单元格,它确实重用了细胞与旧图像。然后你需要每次在 cellForRowAtIndexPath
中显示单元格时更新它:
Since it is a reusable cell, it indeed "reuses" the cell with the old image. Then you need to update it every time the cell is shown in cellForRowAtIndexPath
:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
cell.image=placeholder_image
//then download image
}
这篇关于可重复使用的细胞老图像显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!