From 8d74b8f778d7104df7e3cf0dd513ceae1a69d880 Mon Sep 17 00:00:00 2001 From: Jeff Bailey Date: Sat, 14 Jan 2017 14:14:20 -0800 Subject: [PATCH] Fix for #348 - IGListStackedSectionController's children need to know numberOrItems before didUpdate is called Summary: The reloadData method in IGListStackedSectionController was being called too soon. It was moved from the constructor to the `didUpdateToObject` method. Tests were updated to reflect the change. - [x] All tests pass. Demo project builds and runs. - [x] I added tests, an experiment, or detailed why my change isn't tested. - [ ] I added an entry to the `CHANGELOG.md` for any breaking changes, enhancements, or bug fixes. - [x] I have reviewed the [contributing guide](https://github.com/Instagram/IGListKit/blob/master/.github/CONTRIBUTING.md) Closes https://github.com/Instagram/IGListKit/pull/390 Differential Revision: D4419751 Pulled By: rnystrom fbshipit-source-id: 7c812d306b23dd251c160425873930eb8022b1a5 --- CHANGELOG.md | 1 + Source/IGListStackedSectionController.m | 3 +-- .../IGListStackedSectionControllerInternal.h | 1 + Tests/IGListStackSectionControllerTests.m | 12 +++++++++--- 4 files changed, 12 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fba85a2f..f95614b4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,6 +28,7 @@ This release closes the [2.2.0 milestone](https://github.com/Instagram/IGListKit ### Fixes - Fix bug where emptyView's hidden status is not updated after the number of items is changed with `insertInSectionController:atIndexes:` or related methods. [Peter Edmonston](https://github.com/edmonston) [(#395)](https://github.com/Instagram/IGListKit/pull/395) +- Fix bug where `IGListStackedSectionController`'s children need to know `numberOrItems` before didUpdate is called. [(#348)](https://github.com/Instagram/IGListKit/pull/390) 2.1.0 ----- diff --git a/Source/IGListStackedSectionController.m b/Source/IGListStackedSectionController.m index 07211858..0d590b43 100644 --- a/Source/IGListStackedSectionController.m +++ b/Source/IGListStackedSectionController.m @@ -57,8 +57,6 @@ static void * kStackedSectionControllerIndexKey = &kStackedSectionControllerInde self.displayDelegate = self; self.scrollDelegate = self; self.workingRangeDelegate = self; - - [self reloadData]; } return self; } @@ -152,6 +150,7 @@ static void * kStackedSectionControllerIndexKey = &kStackedSectionControllerInde for (IGListSectionController *sectionController in self.sectionControllers) { [sectionController didUpdateToObject:object]; } + [self reloadData]; } - (void)didSelectItemAtIndex:(NSInteger)index { diff --git a/Source/Internal/IGListStackedSectionControllerInternal.h b/Source/Internal/IGListStackedSectionControllerInternal.h index af10fb24..a2ff41ce 100644 --- a/Source/Internal/IGListStackedSectionControllerInternal.h +++ b/Source/Internal/IGListStackedSectionControllerInternal.h @@ -35,5 +35,6 @@ IGListWorkingRangeDelegate - (IGListSectionController *)sectionControllerForObjectIndex:(NSInteger)itemIndex; - (NSInteger)offsetForSectionController:(IGListSectionController *)sectionController; +- (void)reloadData; @end diff --git a/Tests/IGListStackSectionControllerTests.m b/Tests/IGListStackSectionControllerTests.m index 92ea3df2..26e5df45 100644 --- a/Tests/IGListStackSectionControllerTests.m +++ b/Tests/IGListStackSectionControllerTests.m @@ -85,7 +85,8 @@ static const CGRect kStackTestFrame = (CGRect){{0.0, 0.0}, {100.0, 100.0}}; section4.items = 1; IGListStackedSectionController *stack = [[IGListStackedSectionController alloc] initWithSectionControllers:@[section1, section2, section3, section4]]; - + [stack reloadData]; + XCTAssertEqual([stack numberOfItems], 6); } @@ -100,7 +101,8 @@ static const CGRect kStackTestFrame = (CGRect){{0.0, 0.0}, {100.0, 100.0}}; section4.items = 1; IGListStackedSectionController *stack = [[IGListStackedSectionController alloc] initWithSectionControllers:@[section1, section2, section3, section4]]; - + [stack reloadData]; + XCTAssertEqualObjects([stack sectionControllerForObjectIndex:0], section1); XCTAssertEqualObjects([stack sectionControllerForObjectIndex:1], section1); XCTAssertEqualObjects([stack sectionControllerForObjectIndex:2], section2); @@ -120,6 +122,8 @@ static const CGRect kStackTestFrame = (CGRect){{0.0, 0.0}, {100.0, 100.0}}; section4.items = 1; IGListStackedSectionController *stack = [[IGListStackedSectionController alloc] initWithSectionControllers:@[section1, section2, section3, section4]]; + [stack reloadData]; + XCTAssertEqual([stack offsetForSectionController:section1], 0); XCTAssertEqual([stack offsetForSectionController:section2], 2); XCTAssertEqual([stack offsetForSectionController:section3], 5); @@ -307,10 +311,11 @@ static const CGRect kStackTestFrame = (CGRect){{0.0, 0.0}, {100.0, 100.0}}; IGListDisplayHandler *display = [[IGListDisplayHandler alloc] init]; IGListStackedSectionController *stack = [[IGListStackedSectionController alloc] initWithSectionControllers:@[section1, section2]]; + [stack reloadData]; [display willDisplayCell:cell1 forListAdapter:self.adapter sectionController:stack object:@"a" indexPath:[NSIndexPath indexPathForItem:0 inSection:0]]; [display willDisplayCell:cell2 forListAdapter:self.adapter sectionController:stack object:@"a" indexPath:[NSIndexPath indexPathForItem:1 inSection:0]]; - + [mock1Delegate verify]; [mock2Delegate verify]; } @@ -329,6 +334,7 @@ static const CGRect kStackTestFrame = (CGRect){{0.0, 0.0}, {100.0, 100.0}}; IGListDisplayHandler *display = [[IGListDisplayHandler alloc] init]; IGListStackedSectionController *stack = [[IGListStackedSectionController alloc] initWithSectionControllers:@[section1, section2]]; + [stack reloadData]; // display all 4 cells (2 per child section controller) [display willDisplayCell:cell1 forListAdapter:self.adapter sectionController:stack object:@"a" indexPath:[NSIndexPath indexPathForItem:0 inSection:0]];