简体   繁体   English

在UITableView上调用scrollToRow不适用于AccessoryView

[英]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. 但是,我有一个AccessoryView用于输入新消息。

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. 当我调用scrollToRow(at:index)时,屏幕将滚动到最后一行,但是它不会偏移内容以适应附件视图-即使表视图已连接到附件视图的顶部。

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. 我的annexView是固定的高度,所以也许我可以设置一个tableview偏移量来覆盖它,但是想要一个更优雅的解决方案。

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. 如果将scrollToRow调用延迟一小段时间(这比显示附件视图所需的时间稍长),则滚动将起作用。 Using a value of 0.3 seconds was not enough, but with 0.6 it works beautifully. 使用0.3秒的值是不够的,但是使用0.6秒的值可以很好地工作。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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