问题描述
我有一个包含项目数组的部分数组。这就是我用来使用合成布局填充我的collectionview的东西。我的布局始终可以根据用户进行更改。他们可以删除,添加或修改节或项目。集合视图显示基于数组中节数的节头。 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,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,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()
}
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)