2

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

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 枚举中定义的相同。

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

4

1 回答 1

0

通过枚举附加部分有两种方法:

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

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

var snapshot = NSDiffableDataSourceSnapshot<section,row>
snapShot.appendSection([[topSection,.midSection,. bottomSection]])
dataSource.apply(snapshot)
于 2021-01-12T18:22:19.543 回答