Kirill Kirill - 10 months ago 56
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 Source

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

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