I want to implement sticky headers in my collection view layout, but in a efficient way. I looked into the apple documentation and I found UICollectionViewLayoutInvalidationContext.
I implemented this object along with my custom collection view layout. My goal was to achieve not call prepareLayout
when the collection view scrolls.
I coded the following in my custom collection view layout:
- (BOOL) shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds{
return YES;
}
+ (Class)invalidationContextClass{
return [InvalidationContext class];
}
- (void)invalidateLayoutWithContext:(UICollectionViewLayoutInvalidationContext *)context{
NSMutableArray * indexPaths = [NSMutableArray new];
NSInteger numberOfSections = self.collectionView.numberOfSections;
for (NSInteger section = 0; section < numberOfSections; section++) {
NSInteger numberOfItemsInSection = [self.collectionView numberOfItemsInSection:section];
if (numberOfItemsInSection > 0) {
[indexPaths addObject:[NSIndexPath indexPathForRow:0 inSection:section]];
}
}
[context invalidateItemsAtIndexPaths:indexPaths];
[super invalidateLayoutWithContext:context];
}
And this is my UICollectionViewLayoutInvalidationContext subclass :
#import "InvalidationContext.h"
@implementation InvalidationContext
- (BOOL)invalidateEverything {
return NO;
}
@end
Someone can help me to discover what is wrong?
Note: My sticky headers are the first row for each section. I also was not enable to implement the headers with supplementary views.