我有一个用可变高度的单元格填充的UITableView。当视图被推入视图时,我希望表格滚动到底部。
我目前有以下功能
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[log count]-1 inSection:0];
[self.table scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:NO];log是一个可变数组,包含组成每个单元格内容的对象。
上面的代码在viewDidAppear中工作得很好,但是这有一个不幸的副作用:当视图第一次出现时,它会显示表格的顶部,然后跳到底部。我更希望table view可以在它出现之前滚动到底部。
我在viewWillAppear和viewDidLoad中尝试了滚动,但在这两种情况下,数据都还没有加载到表中,并且都抛出了异常。
任何指导都将非常感谢,即使这只是一个告诉我我所拥有的一切都是可能的情况。
发布于 2017-01-17 21:32:25
swift 3滚动到底部的功能
 override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(false)
        //scroll down
        if lists.count > 2 {
            let numberOfSections = self.tableView.numberOfSections
            let numberOfRows = self.tableView.numberOfRows(inSection: numberOfSections-1)
            let indexPath = IndexPath(row: numberOfRows-1 , section: numberOfSections-1)
            self.tableView.scrollToRow(at: indexPath, at: UITableViewScrollPosition.middle, animated: true)
        }
    }https://stackoverflow.com/questions/2770158
复制相似问题