I am trying to scroll my table view to bottom after adding comments and replies. Comments are sections and replies are rows for that particular section but table view doesn't scroll at the exact bottom of the content after API call for adding comment, I have tried every possible solution available on net. Kindly suggest any solution for the same
Asked
Active
Viewed 1,324 times
0
-
Could you share some code for better explanation? What have you tried before? – emrcftci Mar 05 '20 at 12:06
-
Try using `scrollToRow` method – RJ168 Mar 05 '20 at 12:08
3 Answers
1
This can be done with simple TableView scroll to bottom using IndexPath
tblView.scrollToRow(at: IndexPath(row: 8, section: 0), at: .bottom, animated: true)

RJ168
- 1,006
- 2
- 12
- 22
0
From Jayraj Vala answer
You've calculate the contentSize
of the tableView
to scroll at any particular point Use below code to scroll to bottom of tableView
.
func scrollToBottom() {
let point = CGPoint(x: 0, y: self.tableView.contentSize.height + self.tableView.contentInset.bottom - self.tableView.frame.height)
if point.y >= 0{
self.tableView.setContentOffset(point, animated: animate)
}
}
- Use described func() when you want to scroll
tableView
.

steveSarsawa
- 1,559
- 2
- 14
- 31
-
May I also add that you should call this **after** you call your `reloadData()` function as otherwise it will have the incorrect `contentSize` calculations – Chackle Mar 05 '20 at 12:11
-
Thanks very much for the help, but I have tried above code which doesn't solve my issue – User Tech Mar 05 '20 at 13:59
0
As an extension for UITableView
:
extension UITableView {
func scrollToBottom(withAnimation animated: Bool = true) {
let rowCount = self.numberOfRows(inSection: self.numberOfSections - 1) - 1
// This ensures we don't scroll to the bottom if there is no content
guard rowCount > 0 else { return }
let point = CGPoint(x: 0, y: self.contentSize.height + self.contentInset.bottom - self.bounds.height)
// This ensures we don't scroll to the bottom if all the content is small enough to be displayed without a scroll
guard point.y >= 0 else { return }
self.setContentOffset(point, animated: animated)
}
}
Be sure to call this after tableView.reloadData()
otherwise the table view contentSize
used will be incorrect.

Chackle
- 2,249
- 18
- 34
-
Unfortunately there are no rows for the last section. I want to navigate to last section – User Tech Mar 05 '20 at 13:56
-
@UserTech Just remove the `rowCount` check then. The point and scrolling code will work – Chackle Mar 05 '20 at 14:08
-
Yes I have tried the code `let point = CGPoint(x: 0, y: self.contentSize.height + self.contentInset.bottom - self.bounds.height) // This ensures we don't scroll to the bottom if all the content is small enough to be displayed without a scroll guard point.y >= 0 else { return } self.setContentOffset(point, animated: animated)` but unfortunately it doesn't work for me – User Tech Mar 05 '20 at 14:17
-
@UserTech instead of `self.setContentOffset(point, animated: animated)` can you try `self.contentOffset = point`?. Also try calling `self.layoutIfNeeded()` after too if it still doesn't work. – Chackle Mar 05 '20 at 14:19