From ae523f4fce7500dfb5e081f791064dbc08400acb Mon Sep 17 00:00:00 2001 From: Ethan Nagel Date: Thu, 25 Jun 2015 09:53:36 -0700 Subject: [PATCH] better logic for detecting when the pending index path is before or after the visible index paths --- AsyncDisplayKit/ASTableView.mm | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/AsyncDisplayKit/ASTableView.mm b/AsyncDisplayKit/ASTableView.mm index 86d3b90029..adc757abac 100644 --- a/AsyncDisplayKit/ASTableView.mm +++ b/AsyncDisplayKit/ASTableView.mm @@ -522,25 +522,37 @@ void ASPerformBlockWithoutAnimation(BOOL withoutAnimation, void (^block)()) { if ( _pendingVisibleIndexPath ) { NSMutableSet *indexPaths = [NSMutableSet setWithArray:self.indexPathsForVisibleRows]; - BOOL (^isNextTo)(NSIndexPath *, NSIndexPath *, int) = ^(NSIndexPath *indexPath, NSIndexPath *anchor, int dir) { - if (!anchor) { + BOOL (^isAfter)(NSIndexPath *, NSIndexPath *) = ^BOOL(NSIndexPath *indexPath, NSIndexPath *anchor) { + if (!anchor || !indexPath) { return NO; } if (indexPath.section == anchor.section) { - return (indexPath.row == anchor.row+dir); - } else if (indexPath.section == anchor.section+dir && anchor.row == 0) { - NSIndexPath *toCompare = (dir==1) ? indexPath : anchor; - NSInteger lastRow = [_dataController numberOfRowsInSection:toCompare.section] - 1; - return (toCompare.row == lastRow); - } else { - return NO; + return (indexPath.row == anchor.row+1); // assumes that indexes are valid + + } else if (indexPath.section > anchor.section && indexPath.row == 0) { + if (anchor.row != [_dataController numberOfRowsInSection:anchor.section] -1) { + return NO; // anchor is not at the end of the section + } + + NSInteger nextSection = anchor.section+1; + while([_dataController numberOfRowsInSection:nextSection] == 0) { + ++nextSection; + } + + return indexPath.section == nextSection; } + + return NO; + }; + + BOOL (^isBefore)(NSIndexPath *, NSIndexPath *) = ^BOOL(NSIndexPath *indexPath, NSIndexPath *anchor) { + return isAfter(anchor, indexPath); }; if ( [indexPaths containsObject:_pendingVisibleIndexPath]) { _pendingVisibleIndexPath = nil; // once it has shown up in visibleIndexPaths, we can stop tracking it - } else if (!isNextTo(_pendingVisibleIndexPath, visibleIndexPaths.firstObject, -1) && - !isNextTo(_pendingVisibleIndexPath, visibleIndexPaths.lastObject, +1)) { + } else if (!isBefore(_pendingVisibleIndexPath, visibleIndexPaths.firstObject) && + !isAfter(_pendingVisibleIndexPath, visibleIndexPaths.lastObject)) { _pendingVisibleIndexPath = nil; // not contiguous, ignore. } else { [indexPaths addObject:_pendingVisibleIndexPath];