-1

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.

CarlosAndres
  • 585
  • 1
  • 5
  • 14
  • "implement the headers with supplementary views": that is the way to go. If you have problems, ask a new question therefore and show what you have done, what you expect and what is actually happening. – shallowThought Dec 10 '16 at 12:19
  • The problem is not clear at all. What was your concrete problem? As @shallowThought said, you could explain what you expected and what is actually happening. – javi_swift Jun 07 '19 at 07:54

1 Answers1

-1

Yes, with supplementary view I could implement in a efficient way my custom collection view layout with sticky headers. If someone wants to see my code, feel free to ask it.

CarlosAndres
  • 585
  • 1
  • 5
  • 14
  • Downvote because this answer does not provide any useful information about the topic. You could at least explain what you did. – javi_swift Jun 07 '19 at 07:52