我有我的代码,希望在滚动动画结束后推迟调用reloadData
。
performNextUpdate
。performNextUpdate
。reloadData
之后,然后在最后一个单元格呼叫scrollToRowAtIndexPath
,以播放滚动动画。didEndScrollingAnimation
不会触发,如果该单元格已经在其应滚动到的位置。所以我有canScrollToBottomAtIndexPath
要检查。performNextUpdate
(如果存在)中再次调用pendingDataSource
。我的问题是,很少触发scrollViewDidEndScrollingAnimation
。 animateScrolling = 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);
}
}