簡體   English   中英

如何在UITableView swift 3中使用動畫滾動到底部

[英]How to scroll at the bottom with animated in UITableView swift 3

當UItableView重新加載時,如何為頁腳視圖設置表格視圖的動畫?

我試圖將一個元素追加到tableView,當表視圖重新加載頁腳視圖時,應該附帶一些動畫。 而且,如果表格視圖內容超出屏幕大小,則頁腳視圖應粘貼在屏幕底部。 這是我的嘗試:

    tableView.beginUpdates()
    self.familyAModelArray.append(self.childAModelArray)
    tableView.endUpdates()

注意:這里我想將一個對象附加到一個數組,該數組是UITableView的內容並重新加載TableView。 提前致謝!

要為插入操作設置動畫,需要使用行動畫調用insertRowsAtIndexPaths方法。

將表格單元格元素添加到數組后,必須計算要顯示的新單元格的索引路徑,並創建它們的數組,如下所示

let indexes = [IndexPath(row: 0, section: 0), IndexPath(row: 1, section: 0)]

然后只需調用以下方法為插入設置動畫

tableView.beginUpdates()
tableView.insertRowsAtIndexPaths(indexes, withRowAnimation: .Fade)
tableView.endUpdates()

如果您希望頁腳視圖粘在屏幕底部,那么它不應該是頁腳視圖。 它應該是固定到視圖控制器視圖的自定義UIView。 每當您在tableView上調用reloadData()時,您同樣可以在新的自定義視圖上調用present / animate函數,並將其固定到屏幕底部並在屏幕上顯示動畫。

當UITableView在追加行后重新加載時,我通過在UIView上使用動畫解決了這個問題。

UIView.transition(with: tableView, duration: 0.2, options: .curveEaseIn, animations: {self.tableView.reloadData()}, completion:{ (success) in
            if success {
                if self.familyAModelArray.count >= 3{
                    self.scrollToBottom()
                }
            }
        })

如果我的數組的長度大於3,則通過將UITableView滾動到頂部,頁腳視圖會粘在底部。

func scrollToBottom(){
    DispatchQueue.global(qos: .background).async {
        let indexPath = IndexPath(row: self.familyAModelArray.count-1, section: 0)
        self.tableView.scrollToRow(at: indexPath, at: .top, animated: true)
    }
}

這個解決方案為我的表視圖提供了很好的外觀。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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