diff --git a/Source/IGListCollectionViewLayout.mm b/Source/IGListCollectionViewLayout.mm index af3d62bc..f70d84d1 100644 --- a/Source/IGListCollectionViewLayout.mm +++ b/Source/IGListCollectionViewLayout.mm @@ -123,10 +123,6 @@ static void adjustZIndexForAttributes(UICollectionViewLayoutAttributes *attribut NSMutableArray *result = [NSMutableArray new]; - // since we iterate through sections ascending, we may hit a section who is only partially visible - // in that case we short circuit building layout attributes - BOOL remainingCellsOutOfRect = NO; - const NSRange range = [self rangeOfSectionsInRect:rect]; if (range.location == NSNotFound) { return nil; @@ -152,12 +148,7 @@ static void adjustZIndexForAttributes(UICollectionViewLayoutAttributes *attribut for (NSInteger item = 0; item < itemCount; item++) { NSIndexPath *indexPath = [NSIndexPath indexPathForItem:item inSection:section]; UICollectionViewLayoutAttributes *attributes = [self layoutAttributesForItemAtIndexPath:indexPath]; - if (!CGRectIntersectsRect(attributes.frame, rect)) { - if (remainingCellsOutOfRect) { - return result; - } - } else { - remainingCellsOutOfRect = YES; + if (CGRectIntersectsRect(attributes.frame, rect)) { [result addObject:attributes]; } } diff --git a/Tests/IGListCollectionViewLayoutTests.m b/Tests/IGListCollectionViewLayoutTests.m index bc4c15a5..c7a78f5a 100644 --- a/Tests/IGListCollectionViewLayoutTests.m +++ b/Tests/IGListCollectionViewLayoutTests.m @@ -520,6 +520,64 @@ static const CGRect kTestFrame = (CGRect){{0, 0}, {100, 100}}; XCTAssertEqual([self.layout layoutAttributesForElementsInRect:CGRectMake(0, 250, 100, 1)].count, 1); } +- (void)test_whenSecondItemDoesntIntersectRect_thatOtherAttributesExist { + [self setUpWithStickyHeaders:NO topInset:0]; + NSMutableArray *data = [NSMutableArray new]; + for (NSInteger i = 0; i < 6; i++) { + [data addObject:[[IGLayoutTestSection alloc] initWithInsets:UIEdgeInsetsZero + lineSpacing:0 + interitemSpacing:0 + headerHeight:0 + items:@[ + [[IGLayoutTestItem alloc] initWithSize:(CGSize){50, 100}], + [[IGLayoutTestItem alloc] initWithSize:(CGSize){50, 10}], + ]]]; + } + [self prepareWithData:data]; + + NSArray *attributes = [self.layout layoutAttributesForElementsInRect:CGRectMake(0, 50, 100, 100)]; + NSArray *paths = [[attributes valueForKeyPath:@"indexPath"] sortedArrayUsingSelector:@selector(compare:)]; + NSArray *expectation = @[ + genIndexPath(0, 0), + genIndexPath(1, 0), + genIndexPath(1, 1), + ]; + + // should include 2 of the 100-height items and one of the 10-height + XCTAssertEqualObjects(paths, expectation); +} + +- (void)test_whenTwoConsecutiveItemsDontIntersectRect_thatOtherAttributesExist { + [self setUpWithStickyHeaders:NO topInset:0]; + NSMutableArray *data = [NSMutableArray new]; + for (NSInteger i = 0; i < 6; i++) { + [data addObject:[[IGLayoutTestSection alloc] initWithInsets:UIEdgeInsetsZero + lineSpacing:0 + interitemSpacing:0 + headerHeight:0 + items:@[ + [[IGLayoutTestItem alloc] initWithSize:(CGSize){30, 100}], + [[IGLayoutTestItem alloc] initWithSize:(CGSize){30, 10}], + [[IGLayoutTestItem alloc] initWithSize:(CGSize){30, 10}], + ]]]; + } + [self prepareWithData:data]; + + NSArray *attributes = [self.layout layoutAttributesForElementsInRect:CGRectMake(0, 50, 100, 100)]; + NSArray *paths = [[attributes valueForKeyPath:@"indexPath"] sortedArrayUsingSelector:@selector(compare:)]; + + NSArray *expectation = @[ + genIndexPath(0, 0), + genIndexPath(1, 0), + genIndexPath(1, 1), + genIndexPath(1, 2), + ]; + + // should include 2 of the 100-height items and two of the 10-height + XCTAssertEqualObjects(paths, expectation); +} + + - (void)test_whenChangingBoundsSize_withItemsThatNewlineAfterChange_thatLayoutShiftsItems { [self setUpWithStickyHeaders:NO topInset:0];