Vaibhav Parmar
Vaibhav Parmar

Reputation: 471

Image in UICollectionViewCell getting replaced by image in another Cell

There is a scenario where I'm uploading images, PDF and YouTube URL to the server and after completion, these items are getting displayed in UICollectionView. The image is displayed as it is, for PDF an icon is there and for video, it's thumbnail. If I upload the image first then pdf and then YouTube URL, then in collectionView, the cell with pdf icon is replaced by the image. I'm using Kingfisher for downloading and caching images from the web.

Here is my collectionViewCell code.

override func prepareForReuse() {
    self.imageView.image  = nil
}

func configure(doc: GenericSubmissionDoc) {
    switch doc.type {
    case .document:
        self.imageView.image = UIImage(named: "Pdf-icon")
    case .image:
        self.imageView.setImageFromUrl(urlString: doc.url)
    case .video:
        self.imageView.contentMode = .scaleAspectFit
        guard let thumbnailLink = doc.videoThumbnailUrl else {
            self.imageView.image = UIImage(named: "youtube")
            return
        }
        self.imageView.setImageFromUrl(urlString: thumbnailLink, placeHolder: UIImage(named: "Node-Placeholder"))
    }
}

Upvotes: 0

Views: 145

Answers (1)

Daniel Rinser
Daniel Rinser

Reputation: 8855

I guess you need to cancel the image download in prepareForReuse - just setting the image to nil will not do that.

Consider the following scenario:

  • Cell is used as an image or video cell, image download is triggered
  • Before the download finishes, the cell is reused as a document cell and you set the Pdf-icon image (the download from before is still running!)
  • When the download finishes, it will replace the PDF icon with the downloaded image.

Upvotes: 1

Related Questions