How to scroll to the exact end of the UITableView?

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

    When you push the viewcontroller having the tableview you should scrollTo the specified indexPath only after your Tableview is finished reloading.

    yourTableview.reloadData()
    dispatch_async(dispatch_get_main_queue(), { () -> Void in
        let indexPath = NSIndexPath(forRow: commentArray.count-1, inSection: 0)
      tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: .Bottom, animated: true)
    
    })
    

    The reason for putting the method inside the dispatch_async is once you execute reloadData the next line will get executed immediately and then reloading will happen in main thread. So to know when the tableview gets finished(After all cellforrowindex is finished) we use GCD here. Basically there is no delegate in tableview will tell that the tableview has finished reloading.

提交回复
热议问题