From fb6843d6a29a1d78d9f2e37862fefc05c9f9bd06 Mon Sep 17 00:00:00 2001 From: Mani Ghasemlou Date: Wed, 26 Jul 2017 17:49:22 -0700 Subject: [PATCH] Fix visible section controllers bug (#643) --- CHANGELOG.md | 1 + Source/IGListAdapter.m | 9 +++++---- Tests/IGListAdapterTests.m | 17 +++++++++++++++++ 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 616d56fe0..ea272a08a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ The changelog for `IGListKit`. Also see the [releases](https://github.com/instag ### Fixes - Prevent a crash when update queued immediately after item batch update. [Ryan Nystrom](https://github.com/rnystrom) (tbd) +- Return correct `-[IGListAdapter visibleSectionControllers]` when section has no items, but has supplementary views. [Mani Ghasemlou](https://github.com/manicakes) [(#643)](https://github.com/Instagram/IGListKit/issues/643) ### Enhancements diff --git a/Source/IGListAdapter.m b/Source/IGListAdapter.m index 3d4a50505..46b5efab9 100644 --- a/Source/IGListAdapter.m +++ b/Source/IGListAdapter.m @@ -424,11 +424,12 @@ - (NSArray *)objects { - (NSArray *)visibleSectionControllers { IGAssertMainThread(); - NSArray *visibleCells = [self.collectionView visibleCells]; NSMutableSet *visibleSectionControllers = [NSMutableSet new]; - for (UICollectionViewCell *cell in visibleCells) { - IGListSectionController *sectionController = [self sectionControllerForView:cell]; - IGAssert(sectionController != nil, @"Section controller nil for cell %@", cell); + NSArray *attributes = + [self.collectionView.collectionViewLayout layoutAttributesForElementsInRect:self.collectionView.bounds]; + for (UICollectionViewLayoutAttributes* attribute in attributes) { + IGListSectionController *sectionController = [self sectionControllerForSection:attribute.indexPath.section]; + IGAssert(sectionController != nil, @"Section controller nil for cell in section %zd", attribute.indexPath.section); if (sectionController) { [visibleSectionControllers addObject:sectionController]; } diff --git a/Tests/IGListAdapterTests.m b/Tests/IGListAdapterTests.m index efbefc77d..14a9e1d94 100644 --- a/Tests/IGListAdapterTests.m +++ b/Tests/IGListAdapterTests.m @@ -262,6 +262,23 @@ - (void)test_whenCellsExtendBeyondBounds_thatVisibleSectionControllersAreLimited XCTAssertTrue([visibleSectionControllers containsObject:[self.adapter sectionControllerForObject:@4]]); } +- (void) test_withEmptySectionPlusFooter_thatVisibleSectionControllersAreCorrect { + self.dataSource.objects = @[@0]; + [self.adapter reloadDataWithCompletion:nil]; + IGTestSupplementarySource *supplementarySource = [IGTestSupplementarySource new]; + supplementarySource.dequeueFromNib = YES; + supplementarySource.collectionContext = self.adapter; + supplementarySource.supportedElementKinds = @[UICollectionElementKindSectionFooter]; + IGListSectionController *controller = [self.adapter sectionControllerForObject:@0]; + controller.supplementaryViewSource = supplementarySource; + supplementarySource.sectionController = controller; + [self.adapter performUpdatesAnimated:NO completion:nil]; + NSArray *visibleSectionControllers = [self.adapter visibleSectionControllers]; + + XCTAssertTrue([visibleSectionControllers count] == 1); + XCTAssertTrue(visibleSectionControllers.firstObject.supplementaryViewSource == supplementarySource); +} + - (void)test_whenCellsExtendBeyondBounds_thatVisibleCellsExistForSectionControllers { self.dataSource.objects = @[@2, @3, @4, @5, @6]; [self.adapter reloadDataWithCompletion:nil];