diff --git a/CHANGELOG.md b/CHANGELOG.md index d0aeee8a..23ec38c1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -113,6 +113,8 @@ You can find a [migration guide here](https://instagram.github.io/IGListKit/migr - Fixed a bug where `IGListStackSectionController` passed the wrong section controller for will-drag scroll events. [Ryan Nystrom](https://github.com/rnystrom) [(#286)](https://github.com/Instagram/IGListKit/pull/286) +- Fixed a crash when deselecting a cell through a child section controller in an `IGListStackSectionController`. [Ryan Nystrom](https://github.com/rnystrom) [(#295)](https://github.com/Instagram/IGListKit/pull/295) + 1.0.0 ----- diff --git a/Source/IGListStackedSectionController.m b/Source/IGListStackedSectionController.m index 366a3f25..16665ace 100644 --- a/Source/IGListStackedSectionController.m +++ b/Source/IGListStackedSectionController.m @@ -162,8 +162,8 @@ } - (void)deselectItemAtIndex:(NSInteger)index sectionController:(IGListSectionController *)sectionController animated:(BOOL)animated { - const NSUInteger localIndex = [self localIndexForSectionController:sectionController index:index]; - [self.collectionContext deselectItemAtIndex:localIndex sectionController:self animated:animated]; + const NSUInteger offsetIndex = [self relativeIndexForSectionController:sectionController fromLocalIndex:index]; + [self.collectionContext deselectItemAtIndex:offsetIndex sectionController:self animated:animated]; } - (NSInteger)sectionForSectionController:(IGListSectionController *)sectionController { diff --git a/Tests/IGListStackSectionControllerTests.m b/Tests/IGListStackSectionControllerTests.m index fcfead45..63350f41 100644 --- a/Tests/IGListStackSectionControllerTests.m +++ b/Tests/IGListStackSectionControllerTests.m @@ -686,4 +686,20 @@ static const CGRect kStackTestFrame = (CGRect){{0.0, 0.0}, {100.0, 100.0}}; XCTAssertEqual(self.collectionView.contentOffset.y, 170); } +- (void)test_whenDeselectingChildSectionControllerIndex_thatCorrectCellDeselected { + [self setupWithObjects:@[ + [[IGTestObject alloc] initWithKey:@0 value:@[@1, @2, @3]], + [[IGTestObject alloc] initWithKey:@1 value:@[@1, @1]] + ]]; + + NSIndexPath *path = [NSIndexPath indexPathForItem:1 inSection:1]; + [self.collectionView selectItemAtIndexPath:path animated:NO scrollPosition:UICollectionViewScrollPositionTop]; + XCTAssertTrue([[self.collectionView cellForItemAtIndexPath:path] isSelected]); + + IGListStackedSectionController *stack = [self.adapter sectionControllerForObject:self.dataSource.objects.lastObject]; + IGListSectionController *section = stack.sectionControllers.lastObject; + [section.collectionContext deselectItemAtIndex:0 sectionController:section animated:NO]; + XCTAssertFalse([[self.collectionView cellForItemAtIndexPath:path] isSelected]); +} + @end