UICollectionView's cellForItemAtIndexPath is not being called

后端 未结 30 2247
再見小時候
再見小時候 2020-12-07 11:11

Only my second time using UICollectionView\'s and perhaps I have bitten off more than I can chew but nevertheless:

I am implementing a UICollectionView (myCollection

30条回答
  •  有刺的猬
    2020-12-07 11:33

    The cellForItemAtIndexPath will not get called if you do not provide the content size information to collection view.

    1. If you are using Flow layout: You need to set the item sizes properly.
    2. If you have a custom layout subclassed from UICollectionViewLayout: Ensure you are returning a proper size from the collectionViewContentSize method.

    In case of the latter, you will also observe that your layoutAttributesForElementsRect is also not called. The reason is that you have not specified what is your content size and by default the size will be CGSizeZero. This basically tell collection view that you don't have any content to paint so it does not bother asking you for attributes or cells.

    So, just override collectionViewContentSize and provide a proper size there, it should solve your problem.

提交回复
热议问题