我有一个应用程序,其行为类似于典型的消息应用程序。
假设用户有 100 条消息,我的页面大小为 20。当用户登陆页面时,我显示最后一页(80 - 100 条消息)。
当用户缓慢向上滚动时,当 willDisplay
的 indexPath 变为 indexPath.row = 0 && indexPath.section = 0
时,我会获取上一页 (60 - 80) 。我构建 dataSource 数组并调用 insertRows:at:with:
。我遇到的问题是,当我插入行时,它从 indexPath.row = 0 && indexPath.section = 0
开始,这会导致它触发前一页的预取,从而导致过早预取- 获取其余页面。我可以有一个标志来避免这种情况,但它仍然会按下其余的单元格,导致它失去滚动位置,然后返回,导致闪烁的动画。
我使用rowHeight = UITableViewAutomaticDimension
来计算单元格高度。
我尝试过调用 scrollToRow(at:at:animated)
,但在调用 insertRows:
时它仍然会下推。
我知道这在理论上是可能的,因为 Whatsapp 和 iMessage 等应用程序具有这种行为(假设它们使用 tableView)并且运行良好。
这是我所拥有的要点:
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if !insertingRows && indexPath.section == 0 && indexPath.row == 0 {
loadData()
}
}
private func loadData() {
dataSource.load({ [weak self] (success: Bool, error: Error?) in
guard let strongSelf = self else {
return
}
let newMessagesCount = 20 // testing
var indices = [IndexPath]()
for index in stride(from: 0, to: newMessagesCount, by: 1) {
indices.append(IndexPath(row: index, section: 0))
}
DispatchQueue.main.async {
strongSelf.insertingRows = true
UIView.setAnimationsEnabled(false)
strongSelf.tableView.insertRows(at: indices, with: .none)
UIView.setAnimationsEnabled(true)
strongSelf.insertingRows = false
}
})
}
最佳答案
您可以将元素添加到数据源,而不是使用 tableView 的插入行功能。然后,您可以将 DispatchQueue.main.async 函数中的代码替换为以下代码。
let oldContentHeight: CGFloat = tableView.contentSize.height
let oldOffsetY: CGFloat = tableView.contentOffset.y
tableView.reloadData()
let newContentHeight: CGFloat = tableView.contentSize.height
tableView.contentOffset.y = oldOffsetY + (newContentHeight - oldContentHeight)
请参阅我找到的这个要点以获取更多信息: https://gist.github.com/WorldDownTown/9ed681148e9224f7157c9e8620ce46b8
关于ios - 将行插入到 UITableView 的顶部会导致行向下移动,从而导致动画效果不佳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50008156/