簡體   English   中英

UITableView ScrollToRow函數奇怪地動畫

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM