我正在尝试为我的UICollectionViewDiffableDataSource实现一个collectionView。我的代码编译得很好,但是当我第一次将快照应用到它时,我仍然会遇到这个错误,并出现以下错误:
由于
异常“NSInternalInconsistencyException”终止应用程序,原因:“无效参数不令人满意: self.supplementaryViewProvider \x\x (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
}
}如果需要,我可以张贴完整的调用堆栈。
发布于 2019-11-18 21:22:29
找到答案了。我使用故事板来创建我的collectionView,并且意外地将节标题的属性设置为true。正因为如此,collectionView需要在某个地方提取节标题的视图,但我从未告诉过它在哪里,因此
不满意的
参数: self.supplementaryViewProvider区(self.supplementaryReuseIdentifierProvider && self.supplementaryViewConfigurationHandler)
我在上面找到了一篇很好的文章,供将来任何遇到这个问题的人使用:https://medium.com/@jamesrochabrun/uicollectionviewdiffabledatasource-and-decodable-step-by-step-6b727dd2485
https://stackoverflow.com/questions/58917909
复制相似问题