ios - SDWebImage 错误的单元格图像

标签 ios swift sdwebimage

我使用 SDWebImage pod 进行下载并显示来自 json 的图像,它运行良好,但我有一个大问题。

我正在使用collectionView,当加载单元格时,某些单元格显示错误的图像。我搜索了它,并在 Cell 类的prepareForReuse 函数中尝试了很多解决方案,例如 image = nilsd_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/

相关文章:

ios - React Native 排毒记录用户交互测试脚本

ios - 为什么使用 tableView.dequeueReusableCellWithIdentifier 会出现运行时错误

ios - SDWebImage 单例的使用

ios - UITabBarController 上的 WYPopoverController 下降

swift - 有人可以帮助将 URLSession 从 Swift 转换为 Kotlin

Swift - 使用 URL 下载的低分辨率 firebase 照片

ios - 默认 SDWebImage 缓存大小

iOS状态栏轻触返回App

ios - UICollectionviewcell翻转效果

ios - 在 Swift 中将 Facebook 登录应用于 iOS 应用程序