具有估计高度的CollectionView合成布局仅显示_UICollectionViewOrthogonalScrollerEmbeddedScrollView

问题描述

我的布局很复杂,大多数情况下都可以正常加载。重新加载几次后,尽管一切都消失了(这并不总是发生),我只看到一个空的collectionView。如果我检查它,可以看到只有空的空间和多个_UICollectionViewOrthogonalScrollerEmbeddedScrollView(可以在下面查看布局检查器)。

控制台中的唯一警告与项目插入有关,但我不确定是否甚至相关,因为这些插入不适用于垂直滚动的组合collectionView,但我认为这可能不会导致其他之外的问题。即使collectionView正常显示,警告仍然存在。

发生问题时,collectionView的设置正确,甚至对其进行检查,都可以看到正确设置了节数和行数。如果我在单元格中填充了一个断点,尽管它没有被调用

enter image description here

enter image description here

节的布局示例:

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,count: numberOfItems)
    case .horizontal:
        group = isWide ? NSCollectionLayoutGroup.horizontal(layoutSize: groupSize,count: numberOfItems) : NSCollectionLayoutGroup.horizontal(layoutSize: groupSize,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

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)