简体   繁体   English

UITableView ScrollToRow函数奇怪地动画

[英]UITableView ScrollToRow function animating weirdly

The ScrollToRow is not working as I want. ScrollToRow不能按我的要求工作。 The function I wrote seems to be working fine, but I want it to scroll down to last row only when its added like it happens in chat. 我编写的功能似乎运行良好,但我希望它仅在添加时(如发生在聊天中)才向下滚动到最后一行。 When you get a new message, your tableview scrolls to the bottom to read new message. 收到新消息时,表视图将滚动到底部以阅读新消息。

However, my tableview seems to reload every time before performing scrollToLastRow function (what I mean is every time before scrolling to bottom... it first goes back to top first cell and starts scrolling to bottom from there Every time when a new cell is created). 但是,在执行scrollToLastRow函数之前,我的tableview似乎每次都会重新加载(我的意思是每次滚动到底部之前都在加载...它首先回到顶部的第一个单元格,并从那里开始滚动到底部。每次创建新单元格时)。

My code is simple for the scroll: 我的代码对于滚动很简单:

func scrollToLastRow()
{
    let indexPath = IndexPath(row: messages.count - 1, section: 0)
    self.chat.scrollToRow(at: indexPath, at: .bottom, animated: true)
}

To check my code: here's a link to my repository ChatApplication 要检查我的代码:这是指向我的存储库ChatApplication的链接

the ViewController's used for chat is ---> chatVC the ViewController used to make chat is -> chatCell 用于聊天的ViewController是---> chatVC用于聊天的ViewController是-> chatCell

I have tried anything I could get all over I could find. 我已经尽力尝试了所有可以找到的东西。 Tried dispatch (without it the scroll function wasn't working at all) 尝试调度(没有调度,滚动功能根本不起作用)

Used the function on many different places (including viewDidAppear) Used dispatch.asyncAfter for delay..... but nothing worked. 在许多不同的地方使用了该函数(包括viewDidAppear)。将dispatch.asyncAfter用于延迟.....,但没有任何效果。

Use tableView.insertRows(at: [IndexPath], with: UITableView.RowAnimation) for adding object to end of the tableView. 使用tableView.insertRows(at: [IndexPath], with: UITableView.RowAnimation)将对象添加到tableView的末尾。

From the problem you say and looking in to your code, the animation issues are due to reloading the entire tableView after a new item is added to dataSource array, What you have to do is to use Call tableView.insertRows(at: [IndexPath], with: UITableView.RowAnimation) with the correct indexPath on your tableView and the tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) will be invoked. 根据您说的问题并查看代码, 动画问题是由于在将新项添加到dataSource数组后重新加载了整个tableView而引起的 ,您要做的是使用call tableView.insertRows(at: [IndexPath], with: UITableView.RowAnimation)以及在tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)上具有正确的indexPath和tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)调用。

@Fahad, you are reloading the tableview before the scrollToLast function is getting called. @Fahad,您要在调用scrollToLast函数之前重新加载表视图。 Please check and please verify at your end. 请检查并在最后确认。

Note please update your GitHub repository url in your question properly as it not clickable. 请注意,请正确更新问题中的GitHub存储库网址,因为它不可点击。

Use this function for scrolling to the bottom. 使用此功能滚动到底部。

   func scrollToBottom() {
        let bottomOffset = CGPoint(x: 0, y: contentSize.height - bounds.size.height + contentInset.bottom)
        setContentOffset(bottomOffset, animated: true)
    }

use tableview.scrollToBottom() 使用tableview.scrollToBottom()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM