ios - 刷新后停止 Diffable Data Source 滚动到顶部
问题描述
如何在应用快照后停止将视图滚动到顶部的 diffable 数据源。我目前有这个...
fileprivate func configureDataSource() {
self.datasource = UICollectionViewDiffableDataSource<Section, PostDetail>(collectionView: self.collectionView) {
(collectionView: UICollectionView, indexPath: IndexPath, userComment: PostDetail) -> UICollectionViewCell? in
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: PostDetailCell.reuseIdentifier, for: indexPath) as? PostDetailCell else { fatalError("Cannot create cell")}
cell.user = self.user
cell.postDetail = userComment
cell.likeCommentDelegate = self
return cell
}
var snapshot = NSDiffableDataSourceSnapshot<Section, PostDetail>()
snapshot.appendSections([.main])
snapshot.appendItems(self.userComments)
self.datasource.apply(snapshot, animatingDifferences: true)
}
fileprivate func applySnapshot() {
//let contentOffset = self.collectionView.contentOffset
var snapshot = NSDiffableDataSourceSnapshot<Section, PostDetail>()
snapshot.appendSections([.main])
snapshot.appendItems(self.userComments)
self.datasource.apply(snapshot, animatingDifferences: false)
//self.collectionView.contentOffset = contentOffset
}
存储偏移量,然后重新应用它。有时它可以完美运行,有时视图会跳跃。有没有更好的方法来做到这一点?
解决方案
此问题的根源可能是您的项目标识符类型 UserComment。Diffable 数据源使用您的项目标识符类型的哈希来检测它是新实例还是当前表示的旧实例。如果您手动实现 Hashable 协议,并且使用在初始化该类型的新实例时生成的 UUID,这会误导 Diffable 数据源并告诉它这是项目标识符的新实例。所以必须删除以前的,并代表新的。这会导致表或集合视图在应用快照后滚动。为了解决这个问题,将 uuid 替换为您知道是唯一的类型的属性之一,或者更普遍地使用一种技术为相同的实例生成相同的哈希值。
综上所述,总体思路是将具有相同哈希值的项标识符的实例传递给快照,以告诉 Diffable 数据源这些项不是新的,无需删除以前的项并插入这些项。在这种情况下,您不会遇到不必要的滚动。
推荐阅读
- javascript - 在 html 中使用 java 脚本实现验证时遇到问题
- routing - 比较 here-API-routes:如果部分路线相同,那么腿部航路点是否相同?
- angular7 - 在 Ionic 4,角度 7 中重新加载当前页面
- python - 尴尬的并行For循环,子进程调用
- discord - 向对机器人消息问题作出反应的用户添加角色
- asp.net-core - 当 API ResponseCache NoStore = true 时,HTTP 请求是否被缓存?在特定的 API 端点上
- java - 从List构建树关系图的有效方法
- reactjs - 如何用“链接”包装每个 antd 表行?
- visual-studio-2019 - 在 VS2019 中从 VS 2017 恢复编辑器颜色
- sql-server - 需要根据数据的值将数据透视到 MS SQL 中的对应列