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.")
}
}