【问题标题】:CellProvider Closure never gets executed for UICollectionViewDiffableDataSource永远不会为 UICollectionViewDiffableDataSource 执行 CellProvider 闭包
【发布时间】:2020-08-18 07:16:04
【问题描述】:
class PropertyCollViewController: UIViewController {

    @IBOutlet weak var collectionView: UICollectionView!

    var sections = Person.getSectionData()

    typealias PropertyDataSource = UICollectionViewDiffableDataSource<Person.Section, Property>
    typealias PropertySnapshot = NSDiffableDataSourceSnapshot<Person.Section, Property>
    private var dataSource: PropertyDataSource!

    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "Collections"
        collectionView.collectionViewLayout = configureLayout()
        configureDataSource()
    }



  func configureLayout() -> UICollectionViewCompositionalLayout {
           let size = NSCollectionLayoutSize(
               widthDimension: .fractionalWidth(1.0),
               heightDimension: .absolute(44)
           )
           let item = NSCollectionLayoutItem(layoutSize: size)
           let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(0.5), heightDimension: .absolute(44))

           let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitems: [item])
           let section = NSCollectionLayoutSection(group: group)
           section.contentInsets = NSDirectionalEdgeInsets(top: 10, leading: 10, bottom: 10, trailing: 10)
           section.interGroupSpacing = 0
           return UICollectionViewCompositionalLayout(section: section)
       }

    func configureDataSource()  {
         dataSource = UICollectionViewDiffableDataSource<Person.Section, Property>(collectionView: collectionView, cellProvider: { (collectionView, indexpath, property) -> UICollectionViewCell? in
            print("Cool")
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "PropertyViewCell", for: indexpath) as? PropertyViewCell
            cell?.titleLabel.text = property.asset.name
            cell?.backgroundColor = .red
            return cell
        })


        dataSource.supplementaryViewProvider = { (collectionView, kind, indexpath) in
            guard kind == UICollectionView.elementKindSectionHeader else {
                return nil
            }
            guard let view = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "HeaderView", for: indexpath) as? HeaderView else {
                fatalError()
            }
            let section = self.dataSource.snapshot().sectionIdentifiers[indexpath.section]
            view.sectionTitle.text = section.id.rawValue
            view.backgroundColor = .red
            return view
        }
        var snapShot = PropertySnapshot()
        snapShot.appendSections(sections)
        dataSource.apply(snapShot, animatingDifferences: true)
    }    

}

【问题讨论】:

    标签: ios swift diffabledatasource nsdiffabledatasourcesnapshot


    【解决方案1】:

    这是因为您没有在任何地方附加您的项目,所以您的快照是空的。当您附加一个部分时,您只是将该部分附加为一个标识符。您没有附加该部分及其所有项目。

    请参阅文档here

    【讨论】:

    • 就是这样。我自己想通了。过了一会儿。不过谢谢
    • 你能接受这个作为答案吗?出于某种原因,人们对正确答案投了反对票。
    • 我投了赞成票。不知道为什么会有反对票。此外,我的支持似乎不算数,因为我的声誉很低
    • 完成。对此感到抱歉。
    猜你喜欢
    • 2018-11-01
    • 1970-01-01
    • 2016-10-25
    • 1970-01-01
    • 2019-12-07
    • 1970-01-01
    • 1970-01-01
    • 2020-01-23
    • 1970-01-01
    相关资源
    最近更新 更多