ios - Swift、iOS15、UIKit、CollectionView 标头问题
问题描述
我正在测试iOS15和UIKit的一些新功能。我遇到了一些问题,不知道如何解决。我没有更改该代码。这只是一段与 iOS 14 完美配合的代码,现在更新我的目标后,它会引发错误。
当我为dataSource返回类型为UICollectionElementKindSectionHeader的UICollectionView的自定义标头时,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 中的新功能,但没有看到任何提及该特定代码的任何更改。
任何建议,在该代码中修复什么?
解决方案
这是我想出的部分解决方案。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)
}
}
推荐阅读
- flutter - 如何在flutter中以表格格式显示json格式数据
- python - Python Pandas 中使用列名的宽日期到长数据转换
- mysql - mysql数据目录中的master.000xxx是做什么用的?
- scala - 使用 circe 从 json 数组中删除键
- next.js - useSWR 在 fetcher 函数中返回正确的数据,但在 main 函数中不返回
- r - 错误:`filter()` 输入 `..1` 有问题
- excel - 仅在尚不存在时添加条件格式
- javascript - 如何在 JS 或 Jquery 中向新创建的按钮添加 onclick 事件?
- java - 未创建 Java 中的文本文件
- javascript - 将 hasPendingChanges() 绑定到 View-Model