簡體   English   中英

在swift中向下滾動tableView?

[英]Scroll down tableView in swift?

我有一個表視圖,它從實時數據庫接收數據。 這些數據是從表視圖的底部添加的,因此該表視圖必須向下滾動以顯示新數據。

我找到了一種方法,但是我不滿意,因為滾動始終從列表的頂部開始。 不是很漂亮。

以下是此方法的代碼:

func tableViewScrollToBottom(animated: Bool) {

    let delay = 0.1 * Double(NSEC_PER_SEC)
    let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))

    dispatch_after(time, dispatch_get_main_queue(), {

        let numberOfSections = self.clientTable.numberOfSections
        let numberOfRows = self.clientTable.numberOfRowsInSection(numberOfSections-1)

        if numberOfRows > 0 {
            let indexPath = NSIndexPath(forRow: numberOfRows-1, inSection: (numberOfSections-1))
            self.clientTable.scrollToRowAtIndexPath(indexPath, atScrollPosition: UITableViewScrollPosition.Bottom, animated: animated)
        }

    })
}`

有沒有辦法修改此方法,以便只從前一個位置滾動?

問題可能是行如何插入表中。 例如,如果您使用類似的內容向最后添加行,則會獲得非常流暢的UI:

@IBAction func didTapAddButton(sender: AnyObject) {
    let count = objects.count
    var indexPaths = [NSIndexPath]()

    // add two rows to my model that `UITableViewDataSource` methods reference;
    // also build array of new `NSIndexPath` references

    for row in count ..< count + 2 {
        objects.append("New row \(row)")
        indexPaths.append(NSIndexPath(forRow: row, inSection: 0))
    }

    // now insert and scroll

    tableView.insertRowsAtIndexPaths(indexPaths, withRowAnimation: .None)
    tableView.scrollToRowAtIndexPath(indexPaths.last!, atScrollPosition: .Bottom, animated: true)
}

注意,我不重新加載表,而是調用insertRowsAtIndexPaths 我關掉了動畫因為我知道它們已經關閉了屏幕,然后我會滾動到那一行。

在此輸入圖像描述

暫無
暫無

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

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