4

I have a UICollectionView with UICollectionViewFlowLayout. While testing with large data (5,000+ cells) I noticed a lag when loading. I found out that at loading the sizeForItemAtIndexPath is called for EVERY cell. Since I am doing a messaging like application and each cell is of different height, this causes height estimation to occur. I am not scrolling to the bottom or anything, just loading the view.

I have made a small app demonstrating what I am saying (look at the console to see that it is calling sizeForItemAtIndexPath for all cells) https://github.com/ftheo/CollectionViewLoadingBug

Any help would be appreciated. Thanks

Filip Theo
  • 43
  • 4

2 Answers2

3

Maybe there is no way to prevent calling sizeForItemAtIndexPath method...

That is normal design for calculating scroll content view size.

merge
  • 403
  • 5
  • 10
1

The collection view needs to know what size it's contents have, hence it calls -sizeForItemAtIndexPath:. There is no way around this.

Consider using a UITableView instead. It offers a method -tableView:estimatedHeightForRowAtIndexPath: where you give it a rough guess for offscreen cells.

Christian Schnorr
  • 10,768
  • 8
  • 48
  • 83
  • I don't see why `-sizeForItemAtIndexPath:` is getting called even when you have not scrolled and you are simply at the top, but anyway. I would think it was called only once scrolling/willDisplayCell is called. I ll try tableView. Thanks – Filip Theo Jul 10 '15 at 23:37
  • Imagine you are the collection view. You have to know the size of the contents in order to configure the scroll view correctly (so the scroll bar is displayed appropriately!). How do you know how large it is? You have to ask the delegate for *every single cell*. – Christian Schnorr Jul 11 '15 at 00:45
  • Thanks Christian, makes sense. That's why I would guess there would be an estimate height/width for collectionview as well, not just tableview. If u have a lot of items the scroll bars will be inaccurate either way – Filip Theo Jul 11 '15 at 07:21
  • 2
    @ChristianSchnorr Why does UICollectionViewFlowLayout have estimatedItemSize property, like UITableView have, but works in another way? :( – Bogdan Feb 08 '16 at 13:51