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)
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.
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.
You can add a UITableView to a UIScrollView and have it scroll horizontally.
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With