ios - SDWebImage 错误的单元格图像
问题描述
我使用SDWebImage
pod 进行下载并显示来自 json 的图像,它运行良好,但我有一个大问题。
我collectionView
正在使用,当单元格加载时,一些单元格显示错误的图像。我搜索了它,并尝试了很多解决方案,例如Cell 类的 prepareForReuse func 中的和,但没有工作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 { let 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
}
推荐阅读
- javascript - 从 var js 获取特定的 tagName
- jquery - ngx-datatable 汇总行不适用于列属性中的 summaryFunc。此外,所有汇总值都连接为字符串
- c - 在 MIPS TLB 中,虚拟地址和物理地址之间的混淆
- c# - Timeinterval 在应用程序运行时每 3 分钟检查一次后端是否存在数据库连接 C#
- angularjs - 在 onInit 函数中访问 transclude 槽
- python - 使用日期将 Pandas 数据框合并到多索引
- c++ - 如何在c++中将分数转换为浮点数
- wpf - 向 WPF DecExpress 网格中的单元格添加图标
- linux - 运行 python 脚本时出现问题,该脚本依赖于来自终端的其他文件?
- python - python linux debain的openAi健身房