I have been coding in iOS for around 1.5 years now and during this journey of developing Cocoa View Controllers, I had to deal with UICollectionView many times.
I had always tried to avoid using collection view because of the complexities
As per my experience in dealing with UICollectionView, it is one of the most typical control in Cocoa library. You will encounter too many bugs/errors while trying to incorporate it into your app. Most of the times it happens that we have completed connecting all the parts of collectionView like implementing its dataSource, delegate, etc. but it is still not working.
Same thing happened again to me recently. I was trying to use collection view while developing a new app called "News hub", I will provide details about this app in some other post(After releasing it ;)), and I implemented everything required but still collection view was not shown.
I tried to debug the problem. What I found was:
* delegate and dataSource were correctly setup.
* dataSource's method numberOfSections was returning correct number (in my case, it was returning 1 because I had only one section.)
* dataSource's method numberOfItemsInSection: was being called correctly and it also returned valid number (in my case it is '4')
* But the strange thing was that, dataSource's method cellForItemAtIndexPath: was not being called.
* What I knew till that day was, if the numberOfItemsInSection: method returns value greater that 0, then cellForItemAtIndexPath: will definitely get called.
In my case numberOfItemsInSection: was returning 4 and yet cellForItemAtIndexPath: method was not getting called.
I tried to debug more and then I found the final solution:
I had always tried to avoid using collection view because of the complexities
As per my experience in dealing with UICollectionView, it is one of the most typical control in Cocoa library. You will encounter too many bugs/errors while trying to incorporate it into your app. Most of the times it happens that we have completed connecting all the parts of collectionView like implementing its dataSource, delegate, etc. but it is still not working.
Same thing happened again to me recently. I was trying to use collection view while developing a new app called "News hub", I will provide details about this app in some other post(After releasing it ;)), and I implemented everything required but still collection view was not shown.
I tried to debug the problem. What I found was:
* delegate and dataSource were correctly setup.
* dataSource's method numberOfSections was returning correct number (in my case, it was returning 1 because I had only one section.)
* dataSource's method numberOfItemsInSection: was being called correctly and it also returned valid number (in my case it is '4')
* But the strange thing was that, dataSource's method cellForItemAtIndexPath: was not being called.
* What I knew till that day was, if the numberOfItemsInSection: method returns value greater that 0, then cellForItemAtIndexPath: will definitely get called.
In my case numberOfItemsInSection: was returning 4 and yet cellForItemAtIndexPath: method was not getting called.
I tried to debug more and then I found the final solution:
Final solution:
cellForItemAtIndexPath: method was not being called because of the itemSize for the collectionViewFlowLayout's width was zero (yeah "0").
So, to fix this, I implemented UICollectionViewLayout's method:
Now this method is called for every cell to be shown in the collection view. It returns the valid width of "100 pixels". Now my cellForRowAtIndexPath: Also gets called.
Some more insight
One more thing I got to know while fixing this bug is that cellForRowAtIndexPath: is also not called if the size of the collection view cell returned is more that the collection view itself.