小编mic*_*sox的帖子

刷新后停止 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
    } …
Run Code Online (Sandbox Code Playgroud)

ios swift diffabledatasource

7
推荐指数
1
解决办法
7462
查看次数

Diffable 数据源 - 重新加载标头

可以通过哪些方式UICollectionViewDiffableDataSource重新加载标头?

我有一个集合视图,其中的标题显示用户详细信息,行显示帖子,模型是

struct PostUser {
    var user: User
    var post: Post
}
Run Code Online (Sandbox Code Playgroud)

当我通过快照更改属性时

    var postUsers = [PostUser]() {
        didSet {
            self.applySnapshot(postUsers)
        }
    }

    fileprivate func applySnapshot(_ postsUser: [PostUser]) {
        var snapshot = NSDiffableDataSourceSnapshot<Section, PostUser>()
        snapshot.appendSections([.main])
        snapshot.appendItems(postsUser)
        self.datasource.apply(snapshot, animatingDifferences: true)
    }
Run Code Online (Sandbox Code Playgroud)

行会重新加载,但补充标题不会重新加载。我可以更改标题的唯一方法是使“部分”成为模型的一部分,因此:

    struct Section: Hashable {
        var User: User
    }
Run Code Online (Sandbox Code Playgroud)

我的应用快照现在变成

    fileprivate func applySnapshot(_ postsUser: [PostUser]) {
        var snapshot = NSDiffableDataSourceSnapshot<Section, PostUser>()
        snapshot.appendSections([Section(User: self.user)])
        snapshot.appendItems(postsUser)
        self.datasource.apply(snapshot, animatingDifferences: true)
    }
Run Code Online (Sandbox Code Playgroud)

然后我单独设置用户

    var user: User! = nil {
        didSet {
            self.applySnapshot(self.postUsers) …
Run Code Online (Sandbox Code Playgroud)

ios swift diffabledatasource uicollectionviewdiffabledatasource

4
推荐指数
1
解决办法
2579
查看次数