ScrollToRow 无法正常工作。 我编写的函数似乎工作正常,但我希望它仅在添加时向下滚动到最后一行,就像在聊天中发生的那样。当您收到新消息时,您的表格 View 会滚动到底部以阅读新消息。
但是,我的 tableview 似乎每次都在执行 scrollToLastRow 函数之前重新加载(我的意思是每次滚动到底部之前......它首先回到顶部第一个单元格并从那里开始滚动到底部每次当一个新的单元格被创建)。
我的代码对于滚动来说很简单:
func scrollToLastRow()
{
let indexPath = IndexPath(row: messages.count - 1, section: 0)
self.chat.scrollToRow(at: indexPath, at: .bottom, animated: true)
}
检查我的代码: 这是我的存储库的链接 ChatApplication
用于聊天的 ViewController 是 ---> chatVC 用于聊天的 ViewController 是 -> chatCell
我已经尝试了所有我能找到的东西。 尝试调度(没有它,滚动功能根本不起作用)
在许多不同的地方使用了这个函数(包括 viewDidAppear) 使用 dispatch.asyncAfter 延迟...... 但没有任何效果。
最佳答案
@Fahad,您正在调用scrollToLast 函数之前重新加载表格 View 。请检查并请在您这边进行验证。
请注意,请正确更新问题中的 GitHub 存储库 URL,因为它不可点击。
使用此功能滚动到底部。
func scrollToBottom() {
let bottomOffset = CGPoint(x: 0, y: contentSize.height - bounds.size.height + contentInset.bottom)
setContentOffset(bottomOffset, animated: true)
}
使用 tableview.scrollToBottom()
关于ios - UITableView ScrollToRow 函数奇怪地动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57993605/