From d845a18dfb6f3f223d578c4cc83f9546fc7834c7 Mon Sep 17 00:00:00 2001 From: Levi McCallum Date: Thu, 24 Mar 2016 21:33:01 -0700 Subject: [PATCH] Prevent calculation of current index path when the collection view is empty --- AsyncDisplayKit/ASPagerFlowLayout.m | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/AsyncDisplayKit/ASPagerFlowLayout.m b/AsyncDisplayKit/ASPagerFlowLayout.m index 1c05bcd2fb..a7656af391 100644 --- a/AsyncDisplayKit/ASPagerFlowLayout.m +++ b/AsyncDisplayKit/ASPagerFlowLayout.m @@ -25,10 +25,8 @@ - (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset { if (self.currentIndexPath) { - CGPoint contentOffset = [self _targetContentOffsetForItemAtIndexPath:self.currentIndexPath + return [self _targetContentOffsetForItemAtIndexPath:self.currentIndexPath proposedContentOffset:proposedContentOffset]; - self.currentIndexPath = nil; - return contentOffset; } return [super targetContentOffsetForProposedContentOffset:proposedContentOffset]; @@ -43,6 +41,10 @@ - (NSIndexPath *)_indexPathForVisiblyCenteredItem { + if ([self _dataSourceIsEmpty]) { + return nil; + } + CGRect visibleRect = [self _visibleRect]; CGFloat visibleXCenter = CGRectGetMidX(visibleRect); NSArray *layoutAttributes = [self layoutAttributesForElementsInRect:visibleRect]; @@ -54,6 +56,18 @@ return nil; } +- (BOOL)_dataSourceIsEmpty +{ + if ([self.collectionView numberOfSections]) { + if ([self.collectionView numberOfItemsInSection:0] == 0) { + return YES; + } + } else { + return YES; + } + return NO; +} + - (CGRect)_visibleRect { CGRect visibleRect;