作者:阿爷取的名字-Xx | 来源:互联网 | 2023-01-15 11:33
我正在编写一个表视图,其中在用户交互时添加行.一般行为只是添加一行,然后滚动到表的末尾.
这在iOS11之前工作得很好,但现在滚动总是从表的顶部跳转而不是平滑滚动.
这是与添加新行有关的代码:
func updateLastRow() {
DispatchQueue.main.async {
let lastIndexPath = IndexPath(row: self.currentSteps.count - 1, section: 0)
self.tableView.beginUpdates()
self.tableView.insertRows(at: [lastIndexPath], with: .none)
self.adjustInsets()
self.tableView.endUpdates()
self.tableView.scrollToRow(at: lastIndexPath,
at: UITableViewScrollPosition.none,
animated: true)
}
}
和
func adjustInsets() {
let tableHeight = self.tableView.frame.height + 20
let table40pcHeight = tableHeight / 100 * 40
let bottomInset = tableHeight - table40pcHeight - self.loadedCells.last!.frame.height
let topInset = table40pcHeight
self.tableView.cOntentInset= UIEdgeInsetsMake(topInset, 0, bottomInset, 0)
}
我确信错误在于同时推送多个UI更新(添加行和重新计算边缘插入)这一事实,并尝试使用单独的CATransaction
对象链接这些函数,但这完全混淆了在其他地方定义的异步完成块.更新一些单元格UI元素的代码.
所以任何帮助将不胜感激:)