Invalid result for supplementaryViewForElementKind

Hello,

I'm trying to reset my collectionView's subviews frames and layoutAttributes whenever invalidating the layout for the bottom edge is necessary in layoutSubviews(). However every once in awhile (probably 1/200) I'm getting a crash with the error:

Fatal Exception: NSInternalInconsistencyException
the view returned from -collectionView:viewForSupplementaryElementOfKind:atIndexPath (UICollectionElementKindSectionHeader,<NSIndexPath: 0xe4630e2be7f8d8ab> {length = 2, path = 14 - 0}) was not retrieved by calling -dequeueReusableSupplementaryViewOfKind:withReuseIdentifier:forIndexPath: or is nil (<UICollectionReusableView: 0x15b946bc0; frame = (0 0; 0 0); layer = <CALayer: 0x282f39820>>)

I'm guessing this has something to do with the plain UICollectionReusableView() I am returning in the dequeueSupplementaryView whenever there is no header needed for the section. I've been staring at this for 2 months now so I figured someone might have an idea here :/

Code Block
for (UICollectionViewLayoutAttributes *attributes in attributesInBounds) {
if (!CGRectIntersectsRect(attributes.frame, self.bounds)) {
continue;
}
UICollectionReusableView *view = nil;
switch (attributes.representedElementCategory) {
case UICollectionElementCategoryCell:
view = [self cellForItemAtIndexPath:attributes.indexPath];
break;
case UICollectionElementCategorySupplementaryView:
view = [self supplementaryViewForElementKind:attributes.representedElementKind atIndexPath:attributes.indexPath];
break;
default:
break;
}
[view setFrame:attributes.frame];
[view applyLayoutAttributes:attributes];
}


Invalid result for supplementaryViewForElementKind
 
 
Q