[英]UITableView ScrollToRow function animating weirdly
ScrollToRow不能按我的要求工作。 我编写的功能似乎运行良好,但我希望它仅在添加时(如发生在聊天中)才向下滚动到最后一行。 收到新消息时,表视图将滚动到底部以阅读新消息。
但是,在执行scrollToLastRow函数之前,我的tableview似乎每次都会重新加载(我的意思是每次滚动到底部之前都在加载...它首先回到顶部的第一个单元格,并从那里开始滚动到底部。每次创建新单元格时)。
我的代码对于滚动很简单:
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用于延迟.....,但没有任何效果。
使用tableView.insertRows(at: [IndexPath], with: UITableView.RowAnimation)
将对象添加到tableView的末尾。
根据您说的问题并查看代码, 动画问题是由于在将新项添加到dataSource数组后重新加载了整个tableView而引起的 ,您要做的是使用call tableView.insertRows(at: [IndexPath], with: UITableView.RowAnimation)
以及在tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)
上具有正确的indexPath和tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)
调用。
@Fahad,您要在调用scrollToLast函数之前重新加载表视图。 请检查并在最后确认。
请注意,请正确更新问题中的GitHub存储库网址,因为它不可点击。
使用此功能滚动到底部。
func scrollToBottom() {
let bottomOffset = CGPoint(x: 0, y: contentSize.height - bounds.size.height + contentInset.bottom)
setContentOffset(bottomOffset, animated: true)
}
使用tableview.scrollToBottom()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.