Skw*_*ggs 5 uitableview ios swift ios10 ios11
我正在编写一个表视图,其中在用户交互时添加行.一般行为只是添加一行,然后滚动到表的末尾.
这在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)
}
}
Run Code Online (Sandbox Code Playgroud)
和
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)
}
Run Code Online (Sandbox Code Playgroud)
我确信错误在于同时推送多个UI更新(添加行和重新计算边缘插入)这一事实,并尝试使用单独的CATransaction对象链接这些函数,但这完全混淆了在其他地方定义的异步完成块.更新一些单元格UI元素的代码.
所以任何帮助将不胜感激:)
我只需self.tableView.layoutIfNeeded()在调整插图之前调用即可解决该问题:
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.tableView.endUpdates()
self.tableView.layoutIfNeeded()
self.adjustInsets()
self.tableView.scrollToRow(at: lastIndexPath,
at: UITableViewScrollPosition.bottom,
animated: true)
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1504 次 |
| 最近记录: |