1

我有一个包含一组项目的部分数组。这是我用来使用组合布局填充我的集合视图的方法。我的布局总是可以根据用户而改变。他们可以删除、添加或修改部分或项目。集合视图根据数组中的节数显示节标题。sectionheader 是一个boundarySupplementaryItem。删除部分或从数组中删除所有部分时出现此错误。“由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'当集合视图中只有0个部分时,请求第1部分中补充视图UICollectionElementKindSectionHeader的布局属性'。

如果我注释掉这段代码,它不会崩溃,但节标题不会在那里

let layoutSectionHeader = createSectionHeader()
        layoutSection.boundarySupplementaryItems = [layoutSectionHeader]

这是生成组合布局的代码,以及 didSet 上的sections 数组会发生什么。

    func createCompositionalLayout(sections: [Section]) -> UICollectionViewLayout {
    guard !sections.isEmpty else { return UICollectionViewLayout() }
         let layout = UICollectionViewCompositionalLayout { [weak self]
               sectionIndex, layoutEnvironment in
               let section = sections[sectionIndex]
               switch section.type {
               case .mediumCell:
                return self?.createMediumTableSection(using: section)
               default:
                return self?.createEmptySection(using: section)
               }
           }
         let configuration = UICollectionViewCompositionalLayoutConfiguration()
           configuration.interSectionSpacing = 15
           layout.configuration = configuration
           return layout
       }

 func createMediumTableSection(using section: Section) -> NSCollectionLayoutSection {
        let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), heightDimension: .fractionalHeight(0.33))
        let layoutItem = NSCollectionLayoutItem(layoutSize: itemSize)
        layoutItem.contentInsets = NSDirectionalEdgeInsets(top: 0, leading: 5, bottom: 0, trailing: 5)
        let layoutGroupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(0.93), heightDimension: .fractionalWidth(0.55))
        let layoutGroup = NSCollectionLayoutGroup.vertical(layoutSize: layoutGroupSize, subitems: [layoutItem])
        let layoutSection = NSCollectionLayoutSection(group: layoutGroup)
        layoutSection.orthogonalScrollingBehavior = .groupPagingCentered
        layoutSection.interGroupSpacing = 20
        layoutSection.contentInsets = NSDirectionalEdgeInsets(top: 0, leading: 20, bottom: 0, trailing: 0)
        let layoutSectionHeader = createSectionHeader()
        layoutSection.boundarySupplementaryItems = [layoutSectionHeader]
        return layoutSection
    }

    func createSectionHeader() -> NSCollectionLayoutBoundarySupplementaryItem {
        let layoutSectionHeaderSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(0.93), heightDimension: .estimated(80))
        let layoutSectionHeader = NSCollectionLayoutBoundarySupplementaryItem(layoutSize: layoutSectionHeaderSize, elementKind: UICollectionView.elementKindSectionHeader, alignment: .top)
          return layoutSectionHeader
      }

     func createEmptySection(using section: Section) -> NSCollectionLayoutSection {
        let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), heightDimension: .fractionalHeight(1))
        let layoutItem = NSCollectionLayoutItem(layoutSize: itemSize)
        layoutItem.contentInsets = NSDirectionalEdgeInsets(top: 0, leading: 5, bottom: 0, trailing: 5)
        let layoutGroupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(0.93), heightDimension: .fractionalWidth(0.55))
        let layoutGroup = NSCollectionLayoutGroup.vertical(layoutSize: layoutGroupSize, subitems: [layoutItem])
        let layoutSection = NSCollectionLayoutSection(group: layoutGroup)
        layoutSection.orthogonalScrollingBehavior = .groupPagingCentered
        let layoutSectionHeader = createSectionHeader()
        layoutSection.boundarySupplementaryItems = [layoutSectionHeader]
        return layoutSection
    }
 var sections = [Section]() {
        didSet {
            DispatchQueue.main.async {
                if self.sections.isEmpty {
                    self.mainView.collectionView.backgroundView = MainEmptyView()
                    self.addItemButton.isEnabled = false
                    self.editItemButton.isEnabled = false
                } else {
                    self.mainView.collectionView.backgroundView = nil
                }
            self.mainView.collectionView.collectionViewLayout = self.createCompositionalLayout(sections: self.sections)
                self.mainView.collectionView.reloadData()
            }
        }
    }
4

0 回答 0