UITableView scrollToRowAtIndexPath不会触发scrollViewDidEndScrollingAnimation

时间:2018-10-01 08:23:41

标签: ios uitableview uiscrollview

我有我的代码,希望在滚动动画结束后推迟调用reloadData

  1. 更新数据源时,将调用performNextUpdate
  2. 只有在不播放滚动动画时,我才会performNextUpdate
  3. 呼叫reloadData之后,然后在最后一个单元格呼叫scrollToRowAtIndexPath,以播放滚动动画。
  4. 我知道didEndScrollingAnimation不会触发,如果该单元格已经在其应滚动到的位置。所以我有canScrollToBottomAtIndexPath要检查。
  5. 滚动动画结束后,我将从performNextUpdate(如果存在)中再次调用pendingDataSource

我的问题是,很少触发scrollViewDidEndScrollingAnimationanimateScrolling = YES永远如此。即使有pendingDataSource,该应用也将不再reloadData

有人知道调用scrollViewDidEndScrollingAnimation时触发scrollToRowAtIndexPath:的原因是什么吗?


这是我的示例代码,

- (void)performNextUpdate {
    if (self.pendingDataSource && !self.animateScrolling) {
        self.dataSource = self.pendingDataSource;
        [self.tableView reloadData];
        [self animateScrollToBottomIfNeeded];
    }
}

- (void)animateScrollToBottomIfNeeded {
    if ([self canScrollToBottomAtIndexPath:indexPath]) {
        self.animateScrolling = YES;
        [self.tableView scrollToRowAtIndexPath:lastIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
        // Wait for didEndScrollingAnimation to call before performing the next update
    } else {
        // Can perform the next update immediately, since there's no need to scroll.
        self.animateScrolling = NO;
        [self performNextUpdate];
    }
}

// Sometimes this function is not triggered. 
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
    self.animateScrolling = NO;
    [self performNextUpdate];
}

- (BOOL)canScrollToBottomAtIndexPath:(NSIndexPath *)indexPath {
    if (!indexPath) {
        return NO;
    }
    if ((self.tableView.tracking || self.tableView.dragging) && !self.tableView.isDecelerating) {
        return NO;
    }
    CGRect viewPort = CGRectMake(self.tableView.contentOffset.x, self.tableView.contentOffset.y, self.tableView.bounds.size.width, self.tableView.bounds.size.height);
    CGRect visibleViewPort = UIEdgeInsetsInsetRect(viewPort, self.tableView.contentInset);
    CGRect cellRect = [self.tableView rectForRowAtIndexPath:indexPath];
    if (CGRectEqualToRect(cellRect, CGRectZero)) {
        // If indexPath is invalid, it will return CGRectZero.
        return NO;
    } else {
        return !CGRectContainsRect(visibleViewPort, cellRect);
    }
}

0 个答案:

没有答案