【问题标题】:UICollectionViewDiffableDataSource optional sectionsUICollectionViewDiffableDataSource 可选部分
【发布时间】:2021-01-15 23:56:08
【问题描述】:

我正在尝试使用可区分的数据源实现集合视图。我有一些可选的广告,我想将它们放在集合视图的单独部分中(用于特定布局等)。我的部分如下所示:

enum Section: Int, CaseIterable {
            case topSection
            case midSection
            case advertismentSection
            case bottomSection
        }

advertismentSection 中项目的高度定义如下:

let heightDimension = NSCollectionLayoutDimension.estimated(200)

此 heightDimension 然后用于组高度等。

当我将项目添加到advertismentSection 时,这工作正常,但是,当它为空时,我会假设它的高度应该为 0,但事实并非如此,无论它是否为空,它都是 200。当我尝试将高度设置为 0 时,我收到一条警告说它是无效的。

我尝试的另一种方法是,当我没有要显示的内容时,根本不将此 advertismentSection 添加到快照中,但这会使应用程序崩溃,因为快照中的节数需要与您的 Section 枚举中定义的相同。

我应该如何解决这个问题?

【问题讨论】:

    标签: ios swift uicollectionview diffabledatasource


    【解决方案1】:

    有两种方法可以通过枚举追加部分:

    1. snapshot.appenSections(Section.allcase)
    2. snapshot.appenSections([topSection,.midSection,. bottomSection])
    

    如果您已经应用了快照并且想要不同的部分,那么您必须应用新的快照:

    var snapshot = NSDiffableDataSourceSnapshot<section,row>
    snapShot.appendSection([[topSection,.midSection,. bottomSection]])
    dataSource.apply(snapshot)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多