繁体   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