【发布时间】:2018-08-22 11:09:30
【问题描述】:
应用搜索过滤器后,我们希望 TableView 滚动到结果的顶部。我们有一个随内容滚动的 TableView Header。因此,top 意味着我们希望看到 TableView Header 以及前几个单元格。 我找到了以下内容并尝试了一些适用于 iOS 10 和 iOS 11 的解决方案:UITableView - scroll to the top 但是,我们不断遇到错误。
例如,我们尝试应用 scrollRectToVisible 解决方案,例如:
self.tableView.scrollRectToVisible(CGRect(x: 0, y: 0, width: 1, height: 1), animated: true)
但是在应用了 3 次过滤器后,TableView Header 上方有时会出现如下灰色区域:
Programmatically scroll a UIScrollView to the top of a child UIView (subview) in Swift中TableView Header解决方案的scrollToView也有同样的灰色区域效果。
另一种方法也产生了灰色区域:
UIView.animate(withDuration: 0.25) {
self.tableView.setContentOffset(CGPoint.zero, animated: false)
}
所以,我们尝试了 setContentOffset 解决方案,例如这个:
if #available(iOS 11.0, *) {
tableView.beginUpdates()
tableView.setContentOffset(CGPoint(x: 0, y: -tableView.adjustedContentInset.top), animated: true)
tableView.endUpdates()
} else {
tableView.beginUpdates()
tableView.setContentOffset(CGPoint(x: 0, y: -tableView.contentInset.top), animated: true)
tableView.endUpdates()
}
但是,似乎存在一种情况,它不会将用户一直带到顶部,例如这里:https://youtu.be/OmWCk5PedJE 我不是肯定的,但也许它与下面的单元格有关顶一下。
需要注意的是,TableView 是 Grouped 样式,这样 HeaderView 会向上滚动并离开。我们正在使用较大的估计行高度(800 的巨大数字),并且行高度设置为 UITableViewAutomaticDimension。
Interface Builder 中的文档大纲是:
任何解决方案的想法,将一个带到搜索结果的顶部,而不会有 TableView 标题视图上方灰色区域的副作用,或者如果单元格首先滚动到某个点,它会失败?谢谢。
【问题讨论】:
标签: ios uitableview uiscrollview