EDIT: for anyone having this problem, I've answered my question with a solution that worked for me, that you can find below.
Original Question
I'm trying to build a two-column, multi-section UICollectionView with two types of cells. There are two basic states (per section) I'm aiming for:
- There is at least one item to show
- show at least one cell representing the item, where each cell has half the width of the entire CollectionView
- There is no item to show
- show a special cell that represents the empty state which has a width of twice the normal cell (and thus it occupies the entire width of the CollectionView for that "row")
I've constructed what I believe to be the correct setup for this, including overriding the CollectionViewDelegateFlowLayout
method below:
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return [self trueSectionCount:indexPath.section] > 0 ? CGSizeMake(kItemCellWidth, kItemCellHeight) : CGSizeMake(kItemCellEmptyWidth, kItemCellEmptyHeight);
}
which returns an appropriate size matching some dimensions which are the same as the cells for both states.
My problem is as follows: After the first cell is created in the cellForItemAtIndexPath:
method, and subsequently returned, an assertion failure is thrown, as follows:
*** Assertion failure in -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:], /SourceCache/UIKit_Sim/UIKit-2935.137/UICollectionView.m:1289
After reading some / other / questions on similar topics, I have some theories.
I previously thought it had to do with the fact that I'm using two different sizes, but that can't be right since I'm manually overriding the required method as I showed above, and I'm never setting the itemSize
property of my UICollectionViewFlowLayout
object.
Another theory I had was that this may be caused by some funny business when registering my Nibs/Classes with the UICollectionView
. Since I have two classes which could be used at any given time for item population, and from what I gathered from the comments in UICollectionView.h
, specifically lines 102-104
, there should only be one registered class/nib associated with the view. So I commented out the registration of the nibs and instead of calling dequeueReusableCellWithReuseIdentifier:
in cellForItemAtIndexPath
, I simply initialize a new Cell to nil and then allow my logic to create the new cell from its appropriate nib, as opposed to allowing the CollectionView
to attempt and dequeue or spawn a new cell using it's own internal logic (which I know Apple put in place recently which differentiates a UICollectionView
from a UITableView
in this regard). Unfortunately, I'm still getting this assertion failure.
I've run out of ideas and I'd love to know if anyone can help me with this. Let me know if there is any more information I could have missed and I'll gladly provide it.