Domanda

I'm having a UICollectionView that holds pictures as elements in its datastore.

I want to load a high resolution pic into the element only when its corresponding UICollectionViewCell is currently showing on screen. Later, when the UICollectionViewCell goes off screen, I want to return the element's UIImage into the low resolution version.

My question is, how can I detect when a UICollectionViewCell is going off screen?

(I tried using the prepareForReuse method but I can't predict when it will be called).

I'm currently using a piece of code that sits in scrollViewDidScroll, and every time that the view scrolls I'm checking the self.collectionView.visibleCells to see which cells has scrolled off screen.

It seems a bit of an overhead and I wonder if there is a method called on the UICollectionViewCell itself whenever it is being scrolled of screen ?

È stato utile?

Soluzione

The collectionView:didEndDisplayingCell:forItemAtIndexPath: method on UICollectionViewDelegate should do what you want.

Altri suggerimenti

From Documentation. collectionView:didEndDisplayingCell is called right after it finishes displaying, not when it goes off screen

Use this method to detect when a cell is removed from a collection view, as opposed to monitoring the view itself to see when it disappears

collectionView:didEndDisplayingCell:forItemAtIndexPath: is the correct method to detect when a cell has gone from screen.

Meanwhile, I think it's more correct not to perform cleanup in collectionView:didEndDisplayingCell:forItemAtIndexPath: but to tell your cell to perform cleanup itself:

  func collectionView(_ collectionView: UICollectionView,
                 didEndDisplaying cell: UICollectionViewCell,
                   forItemAt indexPath: IndexPath) {
    cell.prepareForReuse()
  }

With this approach your UICollectionViewDelegate doesn't have to know any implementation details of your UICollectionViewCell subclass. In the cell we'll override prepareForReuse method:

 override func prepareForReuse() {
    super.prepareForReuse()

    imageView.image = lowResolutionImage
    highResolutionImage = nil
 }
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top