How to scroll to the exact end of the UITableView?

前端 未结 16 2580
天涯浪人
天涯浪人 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:14

    Best way to scroll scrollToBottom:

    before call scrollToBottom method call following method

    self.view.layoutIfNeeded()
    
    extension UITableView {
        func scrollToBottom(animated:Bool)  {
            let numberOfRows = self.numberOfRows(inSection: self.numberOfSections - 1) - 1
            if numberOfRows >= 0{
                let indexPath = IndexPath(
                    row: numberOfRows,
                    section: self.numberOfSections - 1)
                self.scrollToRow(at: indexPath, at: .bottom, animated: animated)
            } else {
                let point = CGPoint(x: 0, y: self.contentSize.height + self.contentInset.bottom - self.frame.height)
                if point.y >= 0{
                    self.setContentOffset(point, animated: animated)
                }
            }
        }
    }
    

提交回复
热议问题