UITableView滚动到底部,适用于rowheight==UITableViewAutomaticDimension



假设这里没有答案iOS UITableView滚动至部分底部当表视图行的高度变化时可以正常工作不得不再次提问:如何可靠地滚动到表格底部?意思是:表格内容的底部是一排使用表格底部视图

这个

extension UITableView
{
func scrollToBottom(animated: Bool = true)
{
    layoutSubviews() // if rowHeight UITableViewAutomaticDimension you have to force layout to get semi-correct content size height
    let csh = contentSize.height
    let fsh = bounds.size.height
    if csh > fsh {
        let offset = csh - fsh
        setContentOffset(CGPointMake(0, offset), animated: animated)
    }
}
}

几乎可以使用,但仍然缩小了约150-200像素9.3.1和8.4.1

let offset=csh

它很高兴地过度滚动,在顶部显示了它滚动不足的内容如果let offset=csh-fsh然后是用空填充的wast空白

假定检查了tableview的"垂直反弹"在情节提要中,在后一种情况下它会正确地反弹用户触摸的轻微刺激;-)

这是一个独特的复合体,非常像机器人

这应该很简单,对吧?正确的

scrollToRowAtIndexPath可以处理动态高度的单元格。这里有一个例子:

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
    @IBOutlet weak var tableView: UITableView!
    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.rowHeight = UITableViewAutomaticDimension
        tableView.estimatedRowHeight = 100
    }
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 20
    }
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = UITableViewCell()
        cell.textLabel?.numberOfLines = 0
        cell.textLabel?.lineBreakMode = .ByWordWrapping
        cell.textLabel?.text = [String](count: indexPath.row, repeatedValue: "Hello").joinWithSeparator(" ")
        return cell
    }
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        let lastRow = tableView.numberOfRowsInSection(0)
        tableView.scrollToRowAtIndexPath(NSIndexPath(forRow: lastRow - 1, inSection: 0), atScrollPosition: .Bottom, animated: true)
    }
}

最新更新