ios - UITableView scrollToRowAtIndexPath 不会触发 scrollViewDidEndScrollingAnimation
问题描述
我的代码想要reloadData
在滚动动画完成后推迟调用。
- 当数据源更新
performNextUpdate
时将被调用。 - 我
performNextUpdate
只会在它不播放滚动动画的情况下。 - 调用后
reloadData
,我最后调用scrollToRowAtIndexPath
单元格来播放滚动动画。 - 我知道
didEndScrollingAnimation
如果单元格已经在它应该滚动到的位置,它将不会被触发。所以我必须canScrollToBottomAtIndexPath
检查。 - 滚动动画完成后,我将
performNextUpdate
再次调用 frompendingDataSource
(如果存在)。
我的问题是,很少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);
}
}
解决方案
推荐阅读
- spring - 从 Spring bean 方法返回“this”是否安全?
- java - Spring Boot错误的json值
- android - Android 12 - PROVISIONING_SUCCESSFUL Intent 从未收到
- laravel - 我一直在尝试在 laravel 8 版上使用 unisharp 文件管理器上传图像,这就是我得到的。请帮帮我
- sql-server - 使用 where 子句动态连接表
- javascript - 如何将 JSON.stringify 显示为 HTML 作为值
- php - WordPress:获取也使用第二分类的分类术语
- wordpress - Wordpress 根据用户名分配用户角色
- http - 仅在应用层级别支持 URL 路径的概念?
- java - 使用 Scanner 依赖默认编码