From 36363f42aab61ae1407583db120df3f08f57ddfe Mon Sep 17 00:00:00 2001 From: Brandon Everett Date: Sat, 3 Dec 2016 08:16:29 -0800 Subject: [PATCH] Create private method in IGListStackedSectionController to get relative index Summary: Fixes from PR #273 rebase. jessesquires rnystrom this should look a little better and be able to merge. - [x] All tests pass. Demo project builds and runs. - [x] I added tests, an experiment, or detailed why my change isn't tested. - [x] I have reviewed the [contributing guide](https://github.com/Instagram/IGListKit/blob/master/.github/CONTRIBUTING.md) Closes https://github.com/Instagram/IGListKit/pull/275 Differential Revision: D4271440 Pulled By: rnystrom fbshipit-source-id: a85b8c80b5504640d7f82b183ed5b5d0d0ea5c1b --- Source/IGListStackedSectionController.m | 36 ++++++++++++++----------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/Source/IGListStackedSectionController.m b/Source/IGListStackedSectionController.m index 9e5f5e53..93d39fdc 100644 --- a/Source/IGListStackedSectionController.m +++ b/Source/IGListStackedSectionController.m @@ -109,6 +109,11 @@ static void * kStackedSectionControllerIndexKey = &kStackedSectionControllerInde return index - offset; } +- (NSInteger)relativeIndexForSectionController:(IGListSectionController *)sectionController fromLocalIndex:(NSInteger)index { + const NSUInteger offset = [self offsetForSectionController:sectionController]; + return index + offset; +} + - (NSIndexSet *)itemIndexesForSectionController:(IGListSectionController *)sectionController indexes:(NSIndexSet *)indexes { const NSUInteger offset = [self offsetForSectionController:sectionController]; NSMutableIndexSet *itemIndexes = [[NSMutableIndexSet alloc] init]; @@ -161,8 +166,7 @@ static void * kStackedSectionControllerIndexKey = &kStackedSectionControllerInde } - (UICollectionViewCell *)cellForItemAtIndex:(NSInteger)index sectionController:(IGListSectionController *)sectionController { - const NSUInteger offset = [self offsetForSectionController:sectionController]; - return [self.collectionContext cellForItemAtIndex:(index + offset) sectionController:self]; + return [self.collectionContext cellForItemAtIndex: [self relativeIndexForSectionController:sectionController fromLocalIndex:index] sectionController:self]; } - (NSArray *)visibleCellsForSectionController:(IGListSectionController *)sectionController { @@ -190,52 +194,52 @@ static void * kStackedSectionControllerIndexKey = &kStackedSectionControllerInde - (UICollectionViewCell *)dequeueReusableCellOfClass:(Class)cellClass forSectionController:(IGListSectionController *)sectionController atIndex:(NSInteger)index { - const NSUInteger offset = [self offsetForSectionController:sectionController]; + const NSInteger offsetIndex = [self relativeIndexForSectionController:sectionController fromLocalIndex:index]; return (UICollectionViewCell *_Nonnull)[self.collectionContext dequeueReusableCellOfClass:cellClass forSectionController:self - atIndex:(index + offset)]; + atIndex:offsetIndex]; } - (UICollectionViewCell *)dequeueReusableCellWithNibName:(NSString *)nibName bundle:(NSBundle *)bundle forSectionController:(IGListSectionController *)sectionController atIndex:(NSInteger)index { - const NSUInteger offset = [self offsetForSectionController:sectionController]; + const NSInteger offsetIndex = [self relativeIndexForSectionController:sectionController fromLocalIndex:index]; return (UICollectionViewCell *_Nonnull)[self.collectionContext dequeueReusableCellWithNibName:nibName bundle:bundle forSectionController:self - atIndex:(index + offset)]; + atIndex:offsetIndex]; } - (UICollectionViewCell *)dequeueReusableCellFromStoryboardWithIdentifier:(NSString *)identifier forSectionController:(IGListSectionController *)sectionController atIndex:(NSInteger)index { - const NSUInteger offset = [self offsetForSectionController:sectionController]; + const NSInteger offsetIndex = [self relativeIndexForSectionController:sectionController fromLocalIndex:index]; return (UICollectionViewCell *_Nonnull)[self.collectionContext dequeueReusableCellFromStoryboardWithIdentifier:identifier forSectionController:self - atIndex:(index + offset)]; + atIndex:offsetIndex]; } - (UICollectionReusableView *)dequeueReusableSupplementaryViewOfKind:(NSString *)elementKind forSectionController:(IGListSectionController *)sectionController class:(Class)viewClass atIndex:(NSInteger)index { - const NSUInteger offset = [self offsetForSectionController:sectionController]; + const NSInteger offsetIndex = [self relativeIndexForSectionController:sectionController fromLocalIndex:index]; return (UICollectionViewCell *_Nonnull)[self.collectionContext dequeueReusableSupplementaryViewOfKind:elementKind forSectionController:self class:viewClass - atIndex:(index + offset)]; + atIndex:offsetIndex]; } - (UICollectionReusableView *)dequeueReusableSupplementaryViewFromStoryboardOfKind:(NSString *)elementKind withIdentifier:(NSString *)identifier forSectionController:(IGListSectionController *)sectionController atIndex:(NSInteger)index { - const NSUInteger offset = [self offsetForSectionController:sectionController]; + const NSInteger offsetIndex = [self relativeIndexForSectionController:sectionController fromLocalIndex:index]; return (UICollectionViewCell *_Nonnull)[self.collectionContext dequeueReusableSupplementaryViewFromStoryboardOfKind:elementKind withIdentifier:identifier forSectionController:self - atIndex:(index + offset)]; + atIndex:offsetIndex]; } - (UICollectionReusableView *)dequeueReusableSupplementaryViewOfKind:(NSString *)elementKind @@ -243,12 +247,12 @@ static void * kStackedSectionControllerIndexKey = &kStackedSectionControllerInde nibName:(NSString *)nibName bundle:(NSBundle *)bundle atIndex:(NSInteger)index { - const NSUInteger offset = [self offsetForSectionController:sectionController]; + const NSInteger offsetIndex = [self relativeIndexForSectionController:sectionController fromLocalIndex:index]; return (UICollectionViewCell *_Nonnull)[self.collectionContext dequeueReusableSupplementaryViewOfKind:elementKind forSectionController:self nibName:nibName bundle:bundle - atIndex:(index + offset)]; + atIndex:offsetIndex]; } - (void)reloadInSectionController:(IGListSectionController *)sectionController atIndexes:(NSIndexSet *)indexes { @@ -288,9 +292,9 @@ static void * kStackedSectionControllerIndexKey = &kStackedSectionControllerInde atIndex:(NSInteger)index scrollPosition:(UICollectionViewScrollPosition)scrollPosition animated:(BOOL)animated { - const NSUInteger offset = [self offsetForSectionController:sectionController]; + const NSUInteger offsetIndex = [self relativeIndexForSectionController:sectionController fromLocalIndex:index]; [self.collectionContext scrollToSectionController:self - atIndex:(offset + index) + atIndex:offsetIndex scrollPosition:scrollPosition animated:animated]; }