diff --git a/CHANGELOG.md b/CHANGELOG.md index 616d56fe..ea272a08 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 3d4a5050..46b5efab 100644 --- a/Source/IGListAdapter.m +++ b/Source/IGListAdapter.m @@ -424,11 +424,12 @@ - (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 efbefc77..14a9e1d9 100644 --- a/Tests/IGListAdapterTests.m +++ b/Tests/IGListAdapterTests.m @@ -262,6 +262,23 @@ 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];