【问题标题】:Swift, iOS15, UIKit, CollectionView header issueSwift、iOS15、UIKit、CollectionView 标头问题
【发布时间】:2021-08-28 23:37:53
【问题描述】:

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

当我的 UICollectionView 类型 UICollectionElementKindSectionHeader自定义标头 正在为 dataSource 返回时,Xcode 崩溃>。这是我的代码:

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 的新功能,但没有看到任何提及该特定代码的任何更改。

有什么建议,该代码中要修复什么?

【问题讨论】:

  • 很可能是 iOS 错误。我在所有以前的操作系统版本上都可以正常工作的代码遇到了同样的问题。
  • 你解决了吗?
  • 嘿,是的,我做到了,但是自从我将 Xcode 更新到 Xcode13 后,我遇到了一些其他问题。不过,我会发布解决方案
  • 我看不到你在上面打电话给developer.apple.com/documentation/uikit/uicollectionview/…。您确实在答案代码中调用它。
  • 嗨,马特,我在下面发布了答案。

标签: ios swift wwdc


【解决方案1】:

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

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)
        }
    }

【讨论】:

    猜你喜欢
    • 2023-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-16
    • 2022-08-22
    相关资源
    最近更新 更多