Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cellForItemAt IndexPath does not call if I'm using UICollectionViewDelegateFlowLayout. Is it a bug of iOS?

cellForItemAt indexPath does not call if I'm using UICollectionViewDelegateFlowLayout.

In this case:

class Something: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {

}

it calls my cellforItemAtIndexPath, but does not call my

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
}

but if I include UICollectionViewDelegateFlowLayout:

class Something: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
}

it will call:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
}

but will not call cellForItemAtIndexPath. I do not understand the problem. Is that a bug of iOS or I do not see anything?

like image 366
J. Doe Avatar asked Jan 03 '17 11:01

J. Doe


1 Answers

For me, when creating the collectionView with layout, it should be let layout = UICollectionViewFlowLayout(), not let layout = UICollectionViewLayout() which is so easy to neglect. I was trapped in this like more than twice.

like image 143
David Wu Avatar answered Sep 17 '22 00:09

David Wu