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]; }