自动滚动到具有UITableViewAutomaticDimension行高度的表的底部? - Swift,iOS 8+ [英] Automatically scrolling to the bottom of a table with UITableViewAutomaticDimension row height? - Swift, iOS 8+

查看:673
本文介绍了自动滚动到具有UITableViewAutomaticDimension行高度的表的底部? - Swift,iOS 8+的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常,在表中,以下代码可以自动滚动到底部:

Usually, in a table, the following code can be used to scroll to the bottom automatically:

let indexPath = NSIndexPath(forRow: rowCount - 1, inSection: 0)
self.tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: .Bottom, animated: true)

但是,当我将它设置为使用viewDidLoad()中的自动行高时,如下所示,上面的代码只滚动大约一半的表(根据行高和数目rows - 它只会发生多个行比estimatedRowHeight大得多。)

However, when I set it to use automatic row heights in viewDidLoad() as shown below, the above code only scrolls roughly half way down the table (varies depending on row height and number of rows - it only happens with multiple rows much larger than estimatedRowHeight).

tableView.estimatedRowHeight = 50
tableView.rowHeight = UITableViewAutomaticDimension

在使用UITableViewAutomaticDimension时如何编程滚动到表的底部? >

How can I programatically scroll down to the bottom of a table whilst using UITableViewAutomaticDimension?

推荐答案

我有一个非常类似的问题,当插入行到表视图的底部与自动单元格大小,这是唯一的解决方法,我可以找。它不漂亮,但做的工作(在iOS 8和9测试)。
在插入时:

I had a very similar problem when inserting rows to the bottom of a table view with automatic cell sizing, this is the only workaround I could find. It's not pretty but does the job (tested on iOS 8 and 9). At insertion time:

// compute the insertion index as the last one
let insertionIndex = NSIndexPath(forRow: elements.count - 1, inSection: 0)
// delete from the top, insert at the bottom
tableView.beginUpdates()
tableView.deleteRowsAtIndexPaths(deletionsIndexes, withRowAnimation: .Top)
tableView.insertRowsAtIndexPaths([insertionIndex], withRowAnimation: .Fade)
tableView.endUpdates()
// scroll to the last row, position will be wrong
tableView.scrollToRowAtIndexPath(lastIndex, atScrollPosition: .Bottom, animated: false)
// scroll back to the one before, the actual scrolling will be done after inserting the cell
if elements.count > 1 {
     let prevIndex = NSIndexPath(forRow: lastIndex.row - 1, inSection: 0)
     self.tableView.scrollToRowAtIndexPath(prevIndex, atScrollPosition: .Bottom, animated: false)
}

,你可以看到我们滚动到最后一行,只是触发行插入
然后滚动回到行之前(所有没有动画),以避免任何可见的滚动。
然后当插入单元格时:

as you can see we scroll to the last row just to trigger the row insertion then scroll back to the row before that (all without animation) to avoid any visible scrolling. Then when inserting the cell:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    [...]

    let cell = try configuredCell(forElement: element)
    dispatch_async(dispatch_get_main_queue()) {
        let lastIndex = NSIndexPath(forRow: self.elements.count - 1, inSection: 0)
        // when inserting the last element we do the actual animated scrolling
        if indexPath.row == self.elements.count - 1 {
            tableView.scrollToRowAtIndexPath(lastIndex, atScrollPosition: .Bottom, animated: true)
        }
    }
    return cell
}

这篇关于自动滚动到具有UITableViewAutomaticDimension行高度的表的底部? - Swift,iOS 8+的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆