首页 > 解决方案 > SDWebImage 错误的单元格图像

问题描述

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

collectionView正在使用,当单元格加载时,一些单元格显示错误的图像。我搜索了它,并尝试了很多解决方案,例如Cell 类的 prepareForReuse func 中的和,但没有工作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
    }

标签: iosswiftsdwebimage

解决方案


试试这个

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
}

推荐阅读