【问题标题】:Trying to configure UICollectionViewDiffableDataSource尝试配置 U​​ICollectionViewDiffableDataSource
【发布时间】:2020-05-19 19:24:06
【问题描述】:
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 中的代码不运行。

关于下一步在哪里进行故障排除有什么建议吗?非常感谢!

【问题讨论】:

    标签: swift uicollectionview diffabledatasource


    【解决方案1】:

    需要比这更多的代码。您需要用一些数据 填充 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)
    

    【讨论】:

    • 啊,是的,我已经正确设置了所有内容,但我还没有将任何数据发送到 appendItems。 :facepalm 表情符号: - 谢谢!
    猜你喜欢
    • 2017-03-29
    • 2019-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-02
    • 2018-03-25
    • 2016-05-29
    • 1970-01-01
    相关资源
    最近更新 更多