Calling scrollToRow on UITableView doesn't work with AccessoryView

190 views Asked by At

I have a simple chat interface, where the latest message received from the server appears as the last row on the table, and I need to scroll to this row to show the message to the user. However, I have an AccessoryView which is used for typing a new message.

When I call scrollToRow(at:index) the screen scrolls to the last row, but it does not offset the content to cater for the accessoryView - even though the tableview is connected to the top of the accessory view.

Any ideas how to fix this?? My accessoryView is fixed height, so perhaps I could set a tableview offset to cover it, but wanted a more elegant solution.

1

There are 1 answers

0
theDuncs On

Found the solution. If you delay the scrollToRow call by a short period of time (which is slightly longer than the time needed to present the accessoryView) the scroll will work. Using a value of 0.3 seconds was not enough, but with 0.6 it works beautifully.

DispatchQueue.main.asyncAfter(deadline: .now() + 0.6) {
  self.tableView?.scrollToRow(at: pathToLastRow, at: .bottom, animated: animated)
}