我需要滚动到底部。我有一个像 whatsapp 这样的聊天应用程序。因此,当 View 出现时, TableView 应显示最后一行。我正在通过以下行实现这一点并且效果很好。
tableView.setContentOffset(CGPointMake(0, CGFloat.max), animated: false)
我还需要在出现键盘时滚动到底部。我正在使用自动布局,上面的行不起作用。为此,我使用以下行:
func scrollToLastRow(animated: Bool) {
if self.numberOfRowsInSection(0) > 0 {
self.scrollToRowAtIndexPath(NSIndexPath(forRow: self.numberOfRowsInSection(0) - 1, inSection: 0), atScrollPosition: .Bottom, animated: animated)
}
}
这是 Tableview 的扩展。
当没有太多消息时,此解决方案工作正常。然后我尝试使用 5000 条消息(因此 tableview 有 5000 行,但我正在对它们进行分页)并且当键盘出现时我看到 cpu 使用率为 %98-100。我认为第二个代码是分页问题,它导致将每条消息加载到 ram 并且我的应用程序卡住并收到 ram 警告。
如何在没有任何性能问题的情况下滚动到底部?
最佳答案
如果你有分页,你可以尝试只加载你的当前页面和最后一页,假设你在每页有 20 条消息,在这种情况下你的表只有 40 行。然后你可以使用你的函数:
func scrollToLastRow(animated: Bool) {
if self.numberOfRowsInSection(0) > 0 {
self.scrollToRowAtIndexPath(NSIndexPath(forRow: self.numberOfRowsInSection(0) - 1, inSection: 0), atScrollPosition: .Bottom, animated: animated)
}
关于ios - 在表格 View 和性能中滚动底部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38383408/