我有一个复杂的布局,大部分时间它加载得很好。重新加载几次后,虽然一切都消失了(这并不总是发生),我只看到一个空的 collectionView。如果我检查它,我可以看到只有空空间和多个 _UICollectionViewOrthogonalScrollerEmbeddedScrollView (可以看到下面的布局检查器)。
控制台中的唯一警告与项目插图有关,但我不确定它是否甚至相关,因为插图不适用于具有垂直滚动的组合 collectionView,但我认为它不会导致其他问题那。即使 collectionView 显示正常,警告仍然存在。
当问题发生时,collectionView 设置正确,甚至检查它,您可以看到节和行的数量设置正确。如果我在单元格被填充的位置放置一个断点,尽管它没有被调用。
部分布局示例:
func generateSideBySideAdaptiveLayout(isWide: Bool, height: CGFloat, withHeader header: Bool, numberOfItems: Int, withFooter footer: Bool = false, direction: UICollectionViewAdaptiveLayoutSectionDirection = .vertical) -> NSCollectionLayoutSection {
let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0),
heightDimension: .estimated(height))
let item = NSCollectionLayoutItem(layoutSize: itemSize)
// Show one item on narrow screens, two items on wider screens
let groupFractionalWidth = isWide ? 0.5 : 1
let groupHeight = isWide ? height : height * CGFloat(numberOfItems)
let groupSize = NSCollectionLayoutSize(
widthDimension: .fractionalWidth(CGFloat(groupFractionalWidth)),
heightDimension: .estimated(CGFloat(groupHeight)))
let group: NSCollectionLayoutGroup
switch direction {
case .vertical:
group = isWide ? NSCollectionLayoutGroup.vertical(layoutSize: groupSize, subitem: item, count: numberOfItems / 2) : NSCollectionLayoutGroup.vertical(layoutSize: groupSize, subitem: item, count: numberOfItems)
case .horizontal:
group = isWide ? NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitem: item, count: numberOfItems) : NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitem: item, count: numberOfItems)
}
let section = NSCollectionLayoutSection(group: group)
var supplementary: [NSCollectionLayoutBoundarySupplementaryItem] = []
let headerSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0),
heightDimension: .estimated(44))
if header {
let sectionHeader = NSCollectionLayoutBoundarySupplementaryItem(
layoutSize: headerSize,
elementKind: SectionHeader.title.rawValue, alignment: .top)
supplementary.append(sectionHeader)
}
if footer {
let sectionFooter = NSCollectionLayoutBoundarySupplementaryItem(
layoutSize: headerSize,
elementKind: SectionFooter.actionButton.rawValue,
alignment: .bottom)
supplementary.append(sectionFooter)
}
section.boundarySupplementaryItems = supplementary
section.orthogonalScrollingBehavior = .groupPaging
return section
}
数据设置代码示例:
var snapshot = NSDiffableDataSourceSnapshot<FeedCollectionSection, FeedCollectionItem>()
guard sections.count > 0 else {
return snapshot
}
snapshot.appendSections(sections)
for each in sections {
let items = getItemsForSection(each)
snapshot.appendItems(items, toSection: each)
}
return snapshot