我正在使用 UITableView 构建聊天界面。我使用下面的代码添加单元格
self.tableView.beginUpdates()
self.tableView.insertRows(at: [IndexPath(row: self.messageCards.count - 1, section: 0)], with: .bottom)
self.tableView.endUpdates()
添加后,我使用scrollToRowAtIndexPath来显示最后插入的行。
let indexPath = IndexPath(row: self.messageCards.count - 1, section: 0)
self.tableView.scrollToRow(at: indexPath, at: .bottom, animated: false)
但最后两个动画都混合并且滚动不顺利。
最佳答案
你可以试试这个,我已经测试过代码:
arrayData.addObject(str)
let indexpath = NSIndexPath(forRow: arrayData.count-1, inSection: 0)
tblView.insertRowsAtIndexPaths([indexpath], withRowAnimation: UITableViewRowAnimation.Automatic)
tblView.scrollToRowAtIndexPath(indexpath, atScrollPosition: UITableViewScrollPosition.Bottom, animated: true)
以下是输出:如果您想要完整的源代码,请告诉我。
关于ios - 如何将最后一行添加到 UITableView 并平滑滚动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38304274/