我使用 SDWebImage
pod 进行下载并显示来自 json 的图像,它运行良好,但我有一个大问题。
我正在使用collectionView
,当加载单元格时,某些单元格显示错误的图像。我搜索了它,并在 Cell 类的prepareForReuse 函数中尝试了很多解决方案,例如 image = nil
和 sd_cancelCurrentImageLoad()
但不起作用。
我找不到任何解决方案,请帮助大家,谢谢:)
YazilarCollectionViewCell.swift
import UIKit
import SDWebImage
class YazilarCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var haberGorseli: UIImageView!
@IBOutlet weak var yazarAvatar: UIImageView!
@IBOutlet weak var baslik: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func prepareForReuse() {
super.prepareForReuse()
self.haberGorseli.image = nil
self.haberGorseli.sd_cancelCurrentImageLoad()
}
}
YazilarViewController.swift 中的唯一单元格部分
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "YazilarCollectionViewCell", for: indexPath) as! YazilarCollectionViewCell
if indexPath.row < basliklar.count{
cell.baslik.text = basliklar[indexPath.row].html2String
cell.yazarAvatar.sd_setImage(with: URL(string: catResim[indexPath.row]), placeholderImage: UIImage(named: "faiklogo"))
cell.haberGorseli.sd_setImage(with: URL(string: resimLink[indexPath.row]), placeholderImage: UIImage(named: "faiklogo"))
cell.yazarAvatar.layer.cornerRadius = cell.yazarAvatar.frame.height/2
cell.yazarAvatar.clipsToBounds = true
cell.layer.shadowColor = UIColor.black.cgColor
cell.layer.shadowOpacity = 0.25
cell.layer.shadowOffset = CGSize(width: 0, height: 5)
cell.layer.shadowRadius = 12
cell.layer.masksToBounds = false
}
return cell
}
最佳答案
试试这个
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 让 cell = collectionView.dequeueReusableCell(withReuseIdentifier: "YazilarCollectionViewCell", for: indexPath) as! YazilarCollectionViewCell
if indexPath.row < basliklar.count{
cell.baslik.text = basliklar[indexPath.row].html2String
cell.yazarAvatar.image = UIImage()
cell.yazarAvatar.sd_setImage(with: URL(string: catResim[indexPath.row]), placeholderImage: UIImage(named: "faiklogo"))
cell.haberGorseli.sd_setImage(with: URL(string: resimLink[indexPath.row]), placeholderImage: UIImage(named: "faiklogo"))
cell.yazarAvatar.layer.cornerRadius = cell.yazarAvatar.frame.height/2
cell.yazarAvatar.clipsToBounds = true
cell.layer.shadowColor = UIColor.black.cgColor
cell.layer.shadowOpacity = 0.25
cell.layer.shadowOffset = CGSize(width: 0, height: 5)
cell.layer.shadowRadius = 12
cell.layer.masksToBounds = false
}
return cell
}
关于ios - SDWebImage 错误的单元格图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51439591/