UICollectionView 显示错误的图像

UICollectionView wrong image displayed

我发现了一些关于 UICollectionView 和 iwrong images 问题的帖子,但它们都有一个共同点——人们尝试在 cellForItemAt 方法中下载图像。 我的应用程序包中已经有适当的图像,只需使用 UIImage(named:) 方法将其设置为 UICollectionViewCell。但是我仍然得到错误的图像。谁能帮帮我?

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return liveChannels.count()
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ChannelButtonViewCell", for: indexPath) as! ChannelButtonViewCell


        let current = liveChannels.channels[indexPath.row]
        let currentProgram = current.currentProgram()

        let channelKey = current.channel?.channelKey
        let programDetail = currentProgram?.name

        if let currentProgress = currentProgram?.progress() {
            cell.progressView.setProgress(currentProgress, animated: true)
        }

        print("\(channelKey) - \(current.channel!.logoName)")
        cell.imageName = current.channel!.logoName
        cell.channelTitleLabel.text = channelKey
        cell.channelDetailLabel.text = programDetail

        cell.channelButton.addTarget(self, action: #selector(ChannelsViewController.pressed(_:)), for: .primaryActionTriggered)
        return cell
    }

我正在将 channelKey 和 logoName 打印到控制台,以确保正确的图像存储在 liveChannels.channels 结构中。 (他们确实是正确的)。

这是我的手机:

class ChannelButtonViewCell: UICollectionViewCell {

    @IBOutlet weak var progressView: UIProgressView!
    @IBOutlet weak var channelButton: ChannelButton!
    @IBOutlet weak var channelDetailLabel: UILabel!
    @IBOutlet weak var channelTitleLabel: UILabel!
    @IBOutlet weak var channelImageView: UIImageView!
    var imageName: String?

    override func prepareForReuse() {

        channelImageView.image = nil

        super.prepareForReuse()
    }

    override func draw(_ rect: CGRect) {
        if (self.isSelected) {
            if let imageName  = imageName {
                channelImageView.image = UIImage(named: imageName)
            }

        } else {
            if let imageName  = imageName {
                channelImageView.image = UIImage(named: imageName.appending("_grey"))
            }

        }
    }
}

1) 在极少数情况下你应该覆盖 func draw(_ rect: CGRect) 而你的情况绝对不是那个。

2) 为确保在分配单元格的 var imageName: String? 准确加载和更改图像 ,您可以使用 Swift 的 didSet

3) 为确保您的图像在选择状态更改时更新,您也可以使用 didSet

你最终可能会完全删除 func draw 并像这样添加 didSet 观察者:

class ChannelButtonViewCell: UICollectionViewCell {

    @IBOutlet weak var progressView: UIProgressView!
    @IBOutlet weak var channelButton: ChannelButton!
    @IBOutlet weak var channelDetailLabel: UILabel!
    @IBOutlet weak var channelTitleLabel: UILabel!
    @IBOutlet weak var channelImageView: UIImageView!
    var imageName: String? {
        didSet {
            updateImage()
        }
    }

    override var isSelected: Bool {
        didSet {
            updateImage()
        }
    }

    override func prepareForReuse() {
        channelImageView.image = nil
        super.prepareForReuse()
    }

    func updateImage() {
        guard let imageName = self.imageName else { return }
        if (self.isSelected) {
            channelImageView.image = UIImage(named: imageName)
        } else {
            channelImageView.image = UIImage(named: imageName.appending("_grey"))
        }
    }

}

如果此解决方案无法解决您的问题,请尝试在 func draw() 内部设置断点并检查它何时被调用以及 var imageName

中存储的值