1
func configureDataSource() {
    print("configure dataSource!!!")

    dataSource = UICollectionViewDiffableDataSource
      <Section, StoryItem>(collectionView: storyCollectionView) {
        (collectionView: UICollectionView, indexPath: IndexPath, storyItem: StoryItem) -> UICollectionViewCell? in

    print("try creating a collection view cell!")

显示该函数已被调用的打印语句出现在控制台中,但 { for 中的代码UICollectionViewDiffableDataSource不运行。

关于下一步在哪里进行故障排除的任何建议?非常感谢!

4

1 回答 1

2

需要比这更多的代码。您需要使用一些数据填充diffable 数据源。否则,您的集合视图中的项目数为零,无需生成任何单元格。

一个典型的舞蹈(通常在viewDidLoad)会是这样的:

// make the data source
self.datasource = UICollectionViewDiffableDataSource<String,String>(collectionView:self.collectionView) { 
    cv,ip,s in
    return self.makeCell(cv,ip,s) // return cell
}
// give it a supplementary view provider if you have headers/footers
self.datasource.supplementaryViewProvider = { cv,kind,ip in
    return self.makeSupplementaryView(cv,kind,ip) // return view
}
// give the data source some data (here, my data is sitting in `sections`)
var snap = NSDiffableDataSourceSnapshot<String,String>()
for section in sections {
    snap.appendSections([section.0])
    snap.appendItems(section.1)
}
self.datasource.apply(snap, animatingDifferences: false)
于 2020-02-04T01:41:18.813 回答