ios - Swift、iOS15、UIKit、CollectionView header 问题

标签 ios swift wwdc

我正在测试 iOS15 以及 的一些新功能UIKit .我遇到了一些问题,不知道如何解决。 我没有更改该代码 .这只是一段与 iOS 14 完美配合的代码,现在更新我的目标后,它会引发错误。
当我的 时 Xcode 崩溃了自定义标题 UICollectionView 类型 UICollectionElementKindSectionHeader 正在退回 数据源 .这是我的代码:

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
      }
   }
日志说:

the view returned from -collectionView:viewForSupplementaryElementOfKind:atIndexPath: does not match the element kind it is being used for. When asked for a view of element kind 'FollowersCollectionHeaderView' the data source dequeued a view registered for the element kind 'UICollectionElementKindSectionHeader'.


我确实投了 UICollectionElementKindSectionHeader FollowersCollectionHeaderView ,因此我不确定这里的问题是什么。
我看过 WWDC21 UIKit 中的新功能 但没有看到任何提及该特定代码的任何更改。
任何建议,在该代码中修复什么?

最佳答案

这是我想出的部分解决方案。 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)
        }
    }

关于ios - Swift、iOS15、UIKit、CollectionView header 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67946954/

相关文章:

ios - 在一天中的特定时间从 iOS 将设备位置发送到 Web 服务

ios - 如何在 CloudKit 记录中搜索部分单词?

ios - iOS 10/macOS Sierra 中 Core Data 的 NSPersistentContainer

cocoa - 无法从 2009 Mac WWDC session 中找到用户界面代码示例

iphone - Google Analytics 不显示对我的应用程序的跟踪

iphone - 如何使用 UIBezierPath 在 Cocoa Touch 中绘制多边形

iOS GCM 推送通知并不总是在后台收到

ios - 如何将后退图标添加到左侧栏按钮?

ios - Swift 后台执行选择器

wwdc - WWDC 2018 示例代码可以在线获取吗?