0

我有一个包含部分和开头的集合视图。我想在用户创建数据并在新部分中显示数据后添加一个新部分。这是我尝试过但失败的代码。运行代码后,新部分不会出现。

var snapshot = self.collectionView.diffableDataSource.snapshot()

let recentlySection = Section(title: "Recently Colored",
                                      cells: [userPicture],
                                      sectionType: .recentlyColored)
        
snapshot.appendSections([recentlySection])
snapshot.appendItems([userPicture], toSection: recentlySection)

self.collectionView.diffableDataSource.apply(snapshot, animatingDifferences: true)
self.collectionView.reloadData()

谢谢!

4

1 回答 1

1

我已经解决了我的问题。如果有人遇到同样的问题,我会在这里留下答案。我忘记将新部分添加到我的收藏视图部分列表中。添加这一行解决了我的问题。

self.collectionView.sections.insert(recentlySection, at: 2)
于 2021-08-27T00:21:06.190 回答