Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to scroll to the exact end of the UITableView?

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 view controller.

I have tried with contentOffset and tableView scrollToRowAtIndexPath but still I am not getting the perfect solution for exactly I want.

Can anyone please help me fix this issue?

Here is my code to scroll:

let indexPath = NSIndexPath(forRow: commentArray.count-1, inSection: 0) tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: .Bottom, animated: true) 
like image 387
Padmaja Avatar asked Nov 14 '15 05:11

Padmaja


People also ask

How do I scroll to top Tableview?

To scroll to the top of our tableview we need to create a new IndexPath . This index path has two arguments, row and section . All we want to do is scroll to the top of the table view, therefore we pass 0 for the row argument and 0 for the section argument. UITableView has the scrollToRow method built in.

Is UITableView scrollable?

UITableView scrolls back because it's content size is equal to it's frame (or near to it). If you want to scroll it without returning you need add more cells: table view content size will be large then it's frame.

Can UITableView scroll horizontal?

You can add a UITableView to a UIScrollView and have it scroll horizontally.


Video Answer


1 Answers

For Swift 3.0

Write a function :

func scrollToBottom(){     DispatchQueue.main.async {         let indexPath = IndexPath(row: self.dataArray.count-1, section: 0)         self.tableView.scrollToRow(at: indexPath, at: .bottom, animated: true)     } } 

and call it after you reload the tableview data

tableView.reloadData() scrollToBottom() 
like image 175
Amit Verma Avatar answered Sep 20 '22 22:09

Amit Verma