I'm using a custom layout to get several background color sections using UICollectionReusableView's subclasses. This is the code:
- (void)prepareLayout
{
[super prepareLayout];
self.itemAttributes = [NSMutableArray new];
NSInteger numberOfSection = self.collectionView.numberOfSections;
for (int section = 0; section < numberOfSection; section++) {
if (section == self.decorationViewOfKinds.count)
break;
NSString *decorationViewOfKind = self.decorationViewOfKinds[section % self.decorationViewOfKinds.count];
if ([decorationViewOfKind isKindOfClass:[NSNull class]])
continue;
NSInteger lastIndex = [self.collectionView numberOfItemsInSection:section] - 1;
if (lastIndex < 0)
continue;
UICollectionViewLayoutAttributes *firstItem = [self layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:section]];
UICollectionViewLayoutAttributes *lastItem = [self layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForRow:lastIndex inSection:section]];
CGRect frame = CGRectUnion(firstItem.frame, lastItem.frame);
frame.origin.x -= self.sectionInset.top;
frame.size.height += self.sectionInset.bottom;
if (self.scrollDirection == UICollectionViewScrollDirectionHorizontal) {
frame.size.width += self.sectionInset.left + self.sectionInset.right;
frame.size.height = self.collectionView.frame.size.height;
}
else {
frame.size.width = self.collectionView.frame.size.width;
frame.size.height += self.sectionInset.top + self.sectionInset.bottom;
}
UICollectionViewLayoutAttributes *attributes = [UICollectionViewLayoutAttributes layoutAttributesForDecorationViewOfKind:decorationViewOfKind withIndexPath:[NSIndexPath indexPathForRow:0 inSection:section]];
attributes.zIndex = -1;
attributes.frame = frame;
[self.itemAttributes addObject:attributes];
[self registerNib:[UINib nibWithNibName:decorationViewOfKind bundle:[NSBundle mainBundle]] forDecorationViewOfKind:decorationViewOfKind];
}
}
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
NSMutableArray *attributes = [NSMutableArray arrayWithArray:[super layoutAttributesForElementsInRect:rect]];
for (UICollectionViewLayoutAttributes *attribute in self.itemAttributes) {
if (!CGRectIntersectsRect(rect, attribute.frame))
continue;
[attributes addObject:attribute];
}
return attributes;
}
This code crashes sometimes on a device using iOS 8.4.1 throwing:
Fatal Exception: NSInternalInconsistencyException no UICollectionViewLayoutAttributes instance for -layoutAttributesForDecorationViewOfKindOfKind: AlbumsSectionBackgroundDecoratorView at path {length = 2, path = 2 - 0}
It does not happen in the simulator, just in devices with iOS 8.4.1
Any ideas? Thanks!
EDIT
The bug seems to be happening cause backpressure in the UICollectionView. I have avoided to reload the UICollectionView and seems to be working fine