首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Swift,iOS15,UIKit,CollectionView头问题

Swift,iOS15,UIKit,CollectionView头问题
EN

Stack Overflow用户
提问于 2021-06-12 08:19:22
回答 2查看 1.1K关注 0票数 4

我正在测试iOS15UIKit的一些新功能。我遇到了一些问题,不知道如何解决。,我没有更改代码__。这只是一段与iOS 14完美工作的代码,现在在更新我的目标之后,它会抛出一个错误。

当我的UICollectionView类型UICollectionElementKindSectionHeader自定义头被返回到dataSource时,Xcode崩溃了。这是我的代码:

代码语言:javascript
复制
private func configureDataSource() {
      dataSource = UICollectionViewDiffableDataSource<Section, Follower>(collectionView: collectionView, cellProvider: { (collectionView, indexPath, followers) -> UICollectionViewCell? in
         let cell = collectionView.dequeueReusableCell(withReuseIdentifier: FollowerCell.reuseId, for: indexPath) as! FollowerCell
         cell.set(on: followers)
         return cell
      })
      
      dataSource.supplementaryViewProvider = { (collectionView, kind, indexPath) in
         let header = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionHeader,
                                                                      withReuseIdentifier: FollowersCollectionHeaderView.reuseId,
                                                                      for: indexPath) as! FollowersCollectionHeaderView
         
         header.set(with: self.user)
         return header
      }
   }

日志上写着:

从-collectionView:viewForSupplementaryElementOfKind:atIndexPath:返回的视图与它正在使用的元素类型不匹配。当被问到元素类型'FollowersCollectionHeaderView‘的视图时,数据源将为元素类型'UICollectionElementKindSectionHeader’注册的视图去排队。

我确实将UICollectionElementKindSectionHeader抛给了FollowersCollectionHeaderView,,因此我不确定这里的问题是什么。

我已经看过WWDC21 ,UIKit中的新内容,但是还没有看到对特定代码的任何更改。

如果有什么建议,在代码中要修正什么?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-09-23 19:18:17

这是我想出的部分解决方案。苹果建议使用对象的ID作为collectionView单元格的参考。

代码语言:javascript
复制
enum Section { case main }


var dataSource: UICollectionViewDiffableDataSource<Section, Follower.ID>!

// MARK: - Collection View configurations
    fileprivate lazy var collectionView: UICollectionView = {
        let collectionView = UICollectionView(frame: view.bounds, collectionViewLayout: UIHelper.createCompositionalLayout())
        collectionView.delegate = self
        collectionView.backgroundColor = .systemBackground
        collectionView.register(FollowersCollectionHeaderView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: FollowersCollectionHeaderView.reuseId)
        view.addSubview(collectionView)
        return collectionView
    }()
    
    fileprivate lazy var snapshot: NSDiffableDataSourceSnapshot<Section, Follower.ID> = {
        var snapshot = NSDiffableDataSourceSnapshot<Section, Follower.ID>()
        snapshot.appendSections([.main])
        let itemIdentifiers = followers.map { $0.id }
        snapshot.appendItems(itemIdentifiers, toSection: .main)
        dataSource.apply(snapshot, animatingDifferences: true)
        return snapshot
    }()
    
    fileprivate func updateData(with followers: [Follower]) {
        snapshot = NSDiffableDataSourceSnapshot<Section, Follower.ID>()
        snapshot.appendSections([.main])
        let itemIdentifiers = followers.map { $0.id }
        snapshot.appendItems(itemIdentifiers, toSection: .main)
        dataSource.apply(snapshot, animatingDifferences: true)
    }
    
    fileprivate func configureDataSource() {
        let cellRegistration = UICollectionView.CellRegistration<FollowerCell, Follower.ID> { [weak self]
            cell, indexPath, followerID in
            guard let self = self else { return }
            
            let followerArray = self.followers.filter { $0.id == followerID }
            
            if let follower = followerArray.first {
                cell.set(on: follower)
            }
        }
        
        dataSource = UICollectionViewDiffableDataSource<Section, Follower.ID>(collectionView: collectionView) {
            collectionView, indexPath, itemIdentifier in
            
            return collectionView.dequeueConfiguredReusableCell(using: cellRegistration,
                                                                for: indexPath,
                                                                item: itemIdentifier)
        }
        
        let headerRegistration = createSectionHeaderRegistration()
        dataSource.supplementaryViewProvider = { collectionView, elementKind, indexPath in
            return collectionView.dequeueConfiguredReusableSupplementary(using: headerRegistration, for: indexPath)
        }
    }
    
    fileprivate func createSectionHeaderRegistration() -> UICollectionView.SupplementaryRegistration<FollowersCollectionHeaderView> {
        return UICollectionView.SupplementaryRegistration<FollowersCollectionHeaderView>(
            elementKind: FollowersCollectionHeaderView.reuseId) { [weak self] supplementaryView, elementKind, indexPath in
            guard let self = self else { return }
            supplementaryView.set(with: self.user)
        }
    }
票数 0
EN

Stack Overflow用户

发布于 2022-08-16 03:43:30

使用UICollectionView.elementKindSectionHeader代替创建的字符串

代码语言:javascript
复制
override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
    
    guard let header = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "headerView", for: indexPath) as? TitleDetailsHeaderView else { return UICollectionReusableView() }

return header
}
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67946954

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档