我正在尝试使用具有多个部分的组合布局制作集合视图
但如果部分中有空项目我该如何处理?
如果项目为空,我不想显示该部分
UICollectionViewCompositionalLayout { (section, env) -> NSCollectionLayoutSection? in
// do I have to code in this area?
}
如果您也在使用UICollectionViewDiffableDataSource https://developer.apple.com/documentation/uikit/uicollectionviewdiffabledatasource,您可以在创建/更新快照时处理空部分 - 仅附加其中包含项目的部分。
在我的项目中我做了这样的事情:
func performQuery(animate: Bool) {
var currentSnapshot = NSDiffableDataSourceSnapshot<Section, ViewCell>()
if !calendars.isEmpty {
currentSnapshot.appendSections([Section(name: "main")])
currentSnapshot.appendItems(calendars, toSection: Section(name: "main"))
}
if !lifeCals.isEmpty {
currentSnapshot.appendSections([Section(name: "life")])
currentSnapshot.appendItems(lifeCals, toSection: Section(name: "life"))
}
dataSource.apply(currentSnapshot, animatingDifferences: animate)
}
这样,如果用户的生活日历为 0,则不会有“生活”部分。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)