Swift 如何在uicollectionview中删除sectionHeader?

Swift 如何在uicollectionview中删除sectionHeader?,swift,uicollectionview,uicollectionviewcompositionallayout,Swift,Uicollectionview,Uicollectionviewcompositionallayout,我有一个包含项目数组的节数组。这就是我使用合成布局填充collectionview的方法。我的布局总是可以根据用户的不同而改变。他们可以删除、添加或修改节或项。collectionview根据数组中的节数显示节标题。sectionheader是边界补充项。从数组中删除节或删除所有节时,会出现此错误。“由于未捕获的异常‘NSInternalInconsistencyException’而终止应用程序,原因是:‘当集合视图中只有0个节时,请求辅助视图UICollectionElementKindSe

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

如果我注释掉这段代码,它不会崩溃,但节头不会出现

让layoutSectionHeader=createSectionHeader()
layoutSection.boundarySupplementaryItems=[layoutSectionHeader]
这是生成合成布局以及didSet上的sections数组的代码

func createCompositionLayout(节:[Section])->UICollectionViewLayout{
guard!sections.isEmpty else{return UICollectionViewLayout()}
let layout=UICollectionViewCompositionalLayout{[弱自身]
sectionIndex,中的布局环境
let section=节[sectionIndex]
开关段类型{
病例.中间细胞:
返回self?.createMediumTableSection(使用:节)
违约:
返回self?.createEmptySection(使用:节)
}
}
let configuration=UICollectionViewCompositionalLayoutConfiguration()
configuration.intersectionspace=15
layout.configuration=配置
返回布局
}
func createMediumTableSection(使用section:section)->NSCollectionLayoutSection{
让itemSize=NSCollectionLayoutSize(宽度维度:。分数宽度(1),高度维度:。分数高度(0.33))
let layoutItem=NSCollectionLayoutItem(layoutSize:itemSize)
layoutItem.contentInsets=NSDirectionalEdgeInsets(顶部:0,前导:5,底部:0,尾随:5)
let layoutGroupSize=NSCollectionLayoutSize(宽度维度:。分数宽度(0.93),高度维度:。分数宽度(0.55))
let layoutGroup=NSCollectionLayoutGroup.vertical(layoutSize:layoutGroupSize,子项:[layoutItem])
let layoutSection=NSCollectionLayoutSection(组:layoutGroup)
layoutSection.orthogonalScrollingBehavior=.GroupPagingCenter
layoutSection.interGroupSpacing=20
layoutSection.contentInsets=NSDirectionalEdgeInsets(顶部:0,前导:20,底部:0,尾随:0)
让layoutSectionHeader=createSectionHeader()
layoutSection.boundarySupplementaryItems=[layoutSectionHeader]
返回布局部分
}
func createSectionHeader()->nsCollectionLayoutBoundary补充项{
let layoutSectionHeaderSize=NSCollectionLayoutSize(宽度维度:。分数宽度(0.93),高度维度:。估计值(80))
let layoutSectionHeader=nsCollectionLayoutBoundary补充项(layoutSize:layoutSectionHeaderSize,elementKind:UICollectionView.elementKindSectionHeader,对齐:.top)
返回布局部分标题
}
func createEmptySection(使用section:section)->NSCollectionLayoutSection{
让itemSize=NSCollectionLayoutSize(宽度维度:。分数宽度(1),高度维度:。分数高度(1))
let layoutItem=NSCollectionLayoutItem(layoutSize:itemSize)
layoutItem.contentInsets=NSDirectionalEdgeInsets(顶部:0,前导:5,底部:0,尾随:5)
let layoutGroupSize=NSCollectionLayoutSize(宽度维度:。分数宽度(0.93),高度维度:。分数宽度(0.55))
let layoutGroup=NSCollectionLayoutGroup.vertical(layoutSize:layoutGroupSize,子项:[layoutItem])
let layoutSection=NSCollectionLayoutSection(组:layoutGroup)
layoutSection.orthogonalScrollingBehavior=.GroupPagingCenter
让layoutSectionHeader=createSectionHeader()
layoutSection.boundarySupplementaryItems=[layoutSectionHeader]
返回布局部分
}
var sections=[Section](){
迪塞特{
DispatchQueue.main.async{
如果self.sections.isEmpty{
self.mainView.collectionView.backgroundView=MainEmptyView()
self.addItemButton.isEnabled=false
self.editItemButton.isEnabled=false
}否则{
self.mainView.collectionView.backgroundView=nil
}
self.mainView.collectionView.collectionViewLayout=self.createCompositionLayout(节:self.sections)
self.mainView.collectionView.reloadData()
}
}
}

这是一个测试github项目,显示添加删除以进行调试的代码。这是一个测试github项目,显示添加删除以进行调试的代码。