当我从API获取数据并将其显示在集合视图中时,某些单元格为空白

问题描述

我有一个集合视图,我正在从themoviedb api获取数据。我得到每部电影的演员。每个单元包含个人资料图像视图,名称标签和字符标签。问题是当我获取数据时某些单元格为空白。尽管我已经在单元中配置了所有视图,但问题仍未解决

非常类似于此链接上的问题 CollectionView duplicate cell when loading more data

问题图片

image of the problem

这是cellForItemAt函数

func collectionView(_ collectionView: UICollectionView,cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        switch type {
        case .cast:
            if let castCell = collectionView.dequeueReusableCell(withReuseIdentifier: "CreditsCollectionViewCell",for: indexPath) as? CreditsCollectionViewCell {
                castCell.configureCastCell(with: self.cast[indexPath.row])
                return castCell
            }
            return UICollectionViewCell()
        case .crew:
            if let crewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "CreditsCollectionViewCell",for: indexPath) as? CreditsCollectionViewCell {
                crewCell.configureCrewCell(with: self.crew[indexPath.row])
                return crewCell
            }
            return UICollectionViewCell()
        default:
            return UICollectionViewCell()
        }
    }

CreditsViewController类

import UIKit
import SDWebImage

class CreditsCollectionViewCell: UICollectionViewCell {

    //MARK: - Outlets
    @IBOutlet weak var imageView: UIImageView!
    @IBOutlet weak var nameLabel: UILabel!
    @IBOutlet weak var characterLabel: UILabel!
    @IBOutlet weak var spinner: UIActivityIndicatorView!



    override func prepareForReuse() {
        super.prepareForReuse()
        nameLabel.text = nil
        characterLabel.text = nil
        imageView.image = nil
    }




    //MARK: - Configure cell for the cast
    public func configureCastCell(with cast: MovieCast) {
        if let spinner = spinner {
            spinner.startAnimating()
        }
    
        guard let profilePath = cast.profilePath else {
            return
        }
        let path = "https://image.tmdb.org/t/p/original" + profilePath
        if let profileUrl = URL(string: path) {
            dispatchQueue.main.async {
                self.imageView.sd_setimage(with: profileUrl,completed: nil)
                if let spinner = self.spinner {
                    spinner.removeFromSuperview()
                }
            }
        }
    
        self.nameLabel.text = cast.name
        self.characterLabel.text = cast.character
    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)