How to scroll to the exact end of the UITableView?

前端 未结 16 2516
天涯浪人
天涯浪人 2020-12-07 18:48

I have a UITableView that is populated with cells with dynamic height. I would like the table to scroll to the bottom when the view controller is pushed from vi

16条回答
  •  失恋的感觉
    2020-12-07 19:25

    A little update of @Umair answer in case your tableView is empty

    func scrollToBottom(animated: Bool = true, delay: Double = 0.0) {
        let numberOfRows = tableView.numberOfRows(inSection: tableView.numberOfSections - 1) - 1
        guard numberOfRows > 0 else { return }
    
        DispatchQueue.main.asyncAfter(deadline: .now() + delay) { [unowned self] in
    
            let indexPath = IndexPath(
                row: numberOfRows,
                section: self.tableView.numberOfSections - 1)
            self.tableView.scrollToRow(at: indexPath, at: .bottom, animated: animated)
        }
    }
    

提交回复
热议问题