大佬教程收集整理的这篇文章主要介绍了ios – UITableView:在初始滚动时,使用自定义/可变单元格高度滚动到底部是不准确的,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
所以,我已经看到了很多这方面的答案,但它们似乎都不适合我,我很确定这些也不适用于其他许多人:
tableView.setContentOffset(CGPointMake(0,CGFloat.maX),动画:truE):
砰的一声,我的tableView消失了.
tableView.setContentOffset(CGPointMake(0,self.tableView.contentSize.height – self.tableView.frame.size.height),animated:truE):
不准确的.它并没有一直到底.有时它会超调!
tableView.scrollToRowATindexPath(NSIndexPath(forRow:self.replies.count – 1,inSection:0),atScrollPosition:.bottom,animated:truE)
同样,与前一个一样,这是不准确的.
以下是一些要运行的代码:
class RepliesTableViewController: UITableViewController { var cachedHeights = [Int: CGFloat]() override func viewDidLoad() { super.viewDidLoad() tableView.rowHeight = UITableViewAutomaticDimension } override func tableView(tableView: UITableView,willDisplayCell cell: UITableViewCell,forRowATindexPath indexPath: NSIndexPath) { self.cachedHeights[indexPath.row] = cell.frame.size.height } override func tableView(tableView: UITableView,estimatedHeightForRowATindexPath indexPath: NSIndexPath) -> CGFloat { if let height = cachedHeights[indexPath.row] { return height } else { return 113 } } }
请记住,只有初始滚动到底部并不是一直都是这样.如果我用手指一直向下滚动到底部,然后向上滚动到顶部并触发滚动到底部,这将是完美的.
原因很简单:我将在每个单元格上触发tableView.willDisplayCell,这意味着所有高度都已缓存.这意味着tableView.estimatedHeightForRowATindexPath在初始滚动后完美运行.
但是,如果我从不滚动到底部来缓存所有高度,我尝试以编程方式滚动到底部,它将无法到达那里.为什么?因为我有tableView.estimatedHeightForRowATindexPath返回113.
有些人可能会说我需要更准确的估计,但我拒绝接受这种想法.我的细胞可以小到50,大到5000.没有准确的估计.
我该如何缓解这种情况?
也许我需要在不同的时间缓存所有高峰?现在几点了?我不想对自己做任何计算. tableView.willDisplayCell的美妙之处在于我可以缓存cell.frame.size.height而不是自己繁琐的计算.
编辑2:我有一个搞笑的解决方案……这是非常笨重的…而且是不可接受的……但从技术上讲它是有效的.适合娱乐.
func scrollToBottom() { self.tableView.scrollToRowATindexPath(NSIndexPath(forRow: self.replies.count - 1,inSection: 0),atScrollPosition: .bottom,animated: truE) } override func scrollViewDidEndScrollingAnimation(scrollView: UIScrollView) { if !atBottom { self.tableView.setContentOffset(CGPointMake(0,tableView.contentOffset.y + 200),animated: truE) } } override func scrollViewDidScroll(scrollView: UIScrollView) { atBottom = scrollView.contentOffset.y >= (scrollView.contentSize.height - scrollView.frame.size.height) }
self.tableView.rowHeight = UITableViewAutomaticDimension; self.tableView.estimatedRowHeight = 113.0; // set to whatever your "average" cell height is
这样你的tableCell将重新定位,而不会提到你提到的不准确性
override func tableView(tableView: UITableView,estimatedHeightForRowATindexPath indexPath: NSIndexPath) -> CGFloat { if let height = cachedHeights[indexPath.row] { return height } else { return 113 } }
以上是大佬教程为你收集整理的ios – UITableView:在初始滚动时,使用自定义/可变单元格高度滚动到底部是不准确的全部内容,希望文章能够帮你解决ios – UITableView:在初始滚动时,使用自定义/可变单元格高度滚动到底部是不准确的所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。