Kirill Kirill - 1 month ago 10
iOS Question

View updates too late in Collection View after scrolling

I have CollectionView with image inside each view.
When I'm scrolling this collection, new view (indexPath = 10) contains image from old view (indexPath = 1), then calls this method:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

}


and I set image URL to new view.
What is the best way to hide old image after scrolling before update?

Answer

You need to clean the images in the CollectionViewCell upon reusing them. Override the prepareForReuse function in the CollectionViewCell subclass.

  override func prepareForReuse() {
        super.prepareForReuse()
        // Set the cell's imageView's image to nil
        self.imageView.image = nil
    }
Comments