5

I am creating a UICollectionView with cells of dynamic height and using estimatedItemSize property on UICollectionViewFlowLayout (Apple Docs Reference).

Although, I noticed that once I set the estimatedItemSize property, my prefetchItemsAt method for UICollectionViewDataSourcePrefetching delegate is not getting called.

func collectionView(_ collectionView: UICollectionView, prefetchItemsAt indexPaths: [IndexPath])

If I don't set estimatedItemSize, prefetching starts working again. Is this known behavior or am I doing something wrong here? Please help.

Sample Code:

class CollectionViewController: UICollectionViewController {

var layout: UICollectionViewFlowLayout = {
    let layout = UICollectionViewFlowLayout()
    let width = UIScreen.main.bounds.size.width
    layout.estimatedItemSize = CGSize(width: width, height: 100)
    return layout
}()

override func viewDidLoad() {
    super.viewDidLoad()
    if #available(iOS 10.0, *) {
        collectionView?.prefetchDataSource = self
    }

    collectionView.collectionViewLayout = layout

    // Register cell classes
    self.collectionView.register(UINib.init(nibName: "CollectionViewCell", bundle: nil), forCellWithReuseIdentifier: reuseIdentifier)
}
}

extension CollectionViewController: UICollectionViewDataSourcePrefetching {

// MARK: UICollectionViewDataSourcePrefetching
func collectionView(_ collectionView: UICollectionView, prefetchItemsAt indexPaths: [IndexPath]) {


    // Fetch more cells
    print("prefetchItemsAt called.")
}
}
Elitecoder
  • 170
  • 1
  • 13
  • Does this answer your question? [iOS 10 collectionView:prefetchItemsAt not called](https://stackoverflow.com/questions/38326210/ios-10-collectionviewprefetchitemsat-not-called) – Ely Aug 11 '22 at 11:28

0 Answers0