gpt4 book ai didi

uicollectionview - 将 NSDiffableDataSourceSnapshot 应用于 UICollectionViewDiffableDataSource 导致 'NSInternalInconsistencyException'

转载 作者:行者123 更新时间:2023-12-03 22:02:34 30 4
gpt4 key购买 nike

我正在尝试实现 UICollectionViewDiffableDataSource我的 collectionView .我的代码编译得很好,但是我第一次对它应用快照时一直遇到这个错误,并出现以下错误:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: self.supplementaryViewProvider || (self.supplementaryReuseIdentifierProvider && self.supplementaryViewConfigurationHandler)'



这是我的代码:
    var groups: [Group] = [Group]()
var dataSource: UICollectionViewDiffableDataSource<Section, Group>!

// MARK: - View Life Cycle
override func viewDidLoad() {
super.viewDidLoad()

self.searchBar.delegate = self
self.groups = DummyData.groups

setupDataSource()
}

override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
performSearch(searchQuery: nil)
}


// MARK: - Helper Functions
func performSearch(searchQuery: String?) {
let filteredGroups: [Group]
if let searchQuery = searchQuery, !searchQuery.isEmpty {
filteredGroups = groups.filter { $0.contains(query: searchQuery) }
} else {
filteredGroups = groups
}

var snapshot = NSDiffableDataSourceSnapshot<Section, Group>()
snapshot.appendSections([.main])
snapshot.appendItems(filteredGroups, toSection: .main)
dataSource.apply(snapshot, animatingDifferences: true, completion: nil)
}

func setupDataSource() {
dataSource = UICollectionViewDiffableDataSource <Section, Group>(collectionView: collectionView) { (collectionView: UICollectionView, indexPath: IndexPath, group: Group) -> UICollectionViewCell? in

guard let cell = self.collectionView.dequeueReusableCell(
withReuseIdentifier: String(describing: MyGroupsCollectionViewCell.self), for: indexPath) as? MyGroupsCollectionViewCell else {
fatalError("Cannot create new cell") }

cell.configure(withGroup: group)

return cell
}
}

如果需要,我可以发布完整的调用堆栈。

最佳答案

找到了答案。我正在使用 Storyboard来创建我的 collectionView 并且不小心将 Section Header 的属性设置为 true。因此,collectionView 需要将部分标题的 View 拉到某处,但我从未告诉它在哪里,因此

parameter not satisfying: self.supplementaryViewProvider || (self.supplementaryReuseIdentifierProvider && self.supplementaryViewConfigurationHandler)



这是我为将来遇到此问题的任何人找到的一篇好文章:
https://medium.com/@jamesrochabrun/uicollectionviewdiffabledatasource-and-decodable-step-by-step-6b727dd2485

关于uicollectionview - 将 NSDiffableDataSourceSnapshot 应用于 UICollectionViewDiffableDataSource 导致 'NSInternalInconsistencyException',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58917909/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com