0

I have a collection view and I'm fetching data from themoviedb api. I get the cast for each movie. And each cell contains profile image view, name label and character label. The problem is that some cells are blank when I get the data. Though I've configured all views in the cell, the problem was not resolved.

Very similar to the problem on this link CollectionView duplicate cell when loading more data

Image of the problem image of the problem

here is cellForItemAt function

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 class

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
    }
}
alisengur
  • 1
  • 1
  • What class implements the function `loadProfileImage(from:)`? (So what is `self`?) – Duncan C Sep 01 '20 at 14:09
  • I have a class that named `CreditsCollectionViewCell`, so `configureCastCell` and `loadProfileImage` functions are in this class. – alisengur Sep 01 '20 at 15:06
  • It is a bad idea to load and store model data in your cell instances like that. You should store the data in your model object, and then tell the table view to reload the cell. – Duncan C Sep 01 '20 at 15:30
  • I changed it like you said. But there are still empty cells :( – alisengur Sep 01 '20 at 19:05
  • Edit your question to show your new, still malfunctioning code. – Duncan C Sep 01 '20 at 19:16

0 Answers0