简体   繁体   中英

Calling scrollToRow on UITableView doesn't work with AccessoryView

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.

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)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM