From e66bd85e32662f9375cf0c2c54132e46fde1af6b Mon Sep 17 00:00:00 2001 From: Ryan Nystrom Date: Tue, 6 Dec 2016 18:47:59 -0800 Subject: [PATCH] Fix deselection from stack collection context offset Summary: Offset should be relative. Fixes #279 - [x] All tests pass. Demo project builds and runs. - [x] I added tests, an experiment, or detailed why my change isn't tested. - [x] I have reviewed the [contributing guide](https://github.com/Instagram/IGListKit/blob/master/.github/CONTRIBUTING.md) Closes https://github.com/Instagram/IGListKit/pull/295 Differential Revision: D4290143 Pulled By: rnystrom fbshipit-source-id: 8d6dccb821b12700d0d7e8704006ef5371396dfd --- CHANGELOG.md | 2 ++ Source/IGListStackedSectionController.m | 4 ++-- Tests/IGListStackSectionControllerTests.m | 16 ++++++++++++++++ 3 files changed, 20 insertions(+), 2 deletions(-) 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