【问题标题】:Applying NSDiffableDataSourceSnapshot to UICollectionViewDiffableDataSource causes 'NSInternalInconsistencyException'将 NSDiffableDataSourceSnapshot 应用于 UICollectionViewDiffableDataSource 会导致“NSInternalInconsistencyException”
【发布时间】:2020-03-14 00:29:04
【问题描述】:

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

由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'无效参数不满足: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
        }
    }

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

【问题讨论】:

    标签: uicollectionview uikit swift5 diffabledatasource nsdiffabledatasourcesnapshot


    【解决方案1】:

    找到了答案。我正在使用情节提要创建我的 collectionView 并且不小心将 Section Header 的属性设置为 true。因此,collectionView 需要将部分标题的视图拉到某个地方,但我从来没有告诉它在哪里,因此

    参数不满足:self.supplementaryViewProvider || (self.supplementaryReuseIdentifierProvider && self.supplementaryViewConfigurationHandler)

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

    【讨论】:

      猜你喜欢
      • 2021-07-02
      • 1970-01-01
      • 2019-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-28
      • 2017-12-05
      相关资源
      最近更新 更多