首页 > 解决方案 > 刷新后停止 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
    }

存储偏移量,然后重新应用它。有时它可以完美运行,有时视图会跳跃。有没有更好的方法来做到这一点?

标签: iosswiftdiffabledatasource

解决方案


此问题的根源可能是您的项目标识符类型 UserComment。Diffable 数据源使用您的项目标识符类型的哈希来检测它是新实例还是当前表示的旧实例。如果您手动实现 Hashable 协议,并且使用在初始化该类型的新实例时生成的 UUID,这会误导 Diffable 数据源并告诉它这是项目标识符的新实例。所以必须删除以前的,并代表新的。这会导致表或集合视图在应用快照后滚动。为了解决这个问题,将 uuid 替换为您知道是唯一的类型的属性之一,或者更普遍地使用一种技术为相同的实例生成相同的哈希值。

综上所述,总体思路是将具有相同哈希值的项标识符的实例传递给快照,以告诉 Diffable 数据源这些项不是新的,无需删除以前的项并插入这些项。在这种情况下,您不会遇到不必要的滚动。


推荐阅读