2

I want to learn using UICollectionViewDiffableDataSource using Pinterest Layout, but when I try to running my simulator. it crash and give me a message

request for number of items in section 0 when there are only 0 sections in the collection view

I did the Pinterest layout using raywenderlich tutorial , when I stumble in google the problem cause is the Pinterest layout. but before I use the diffableDataSource it works fine, but after using diffableDataSource it crash. where do I do wrong? can you help me, this is my code for diffableDataSource

    // This is in my PhotoViewController
    enum Section {
        case main
    }

    let layout = PinterestLayout()
    var dataSource: UICollectionViewDiffableDataSource<Section, PhotoItem>!
    var photos: [PhotoItem] = []

    override func viewDidLoad() {
        super.viewDidLoad()
        navigationItem.title = "Photos"
        navigationController?.navigationBar.prefersLargeTitles = true
        setupCollectionView()
        getPhoto()
        configureDataSource()
    }

    private func getPhoto() {
        NetworkManager.shared.getPhotos(matching: "office", page: 1) { [weak self] result in
            guard let self = self else { return }

            switch result {
            case .success(let photos):
                self.photos = photos
                self.updateData()
            case .failure(let error):
                print(error)
            }
        }
    }

    private func configureDataSource() {
        dataSource = UICollectionViewDiffableDataSource<Section, PhotoItem>(collectionView: collectionView, cellProvider: { (collectionView, indexPath, photoItems) -> UICollectionViewCell? in
            let cell = collectionView.dequeueReusableCell(withClass: PhotoCell.self, for: indexPath)
            cell.set(photoItems)
            return cell
        })
    }

    private func updateData() {
        var snapshot = NSDiffableDataSourceSnapshot<Section, PhotoItem>()
        snapshot.appendSections([.main])
        snapshot.appendItems(photos)
        DispatchQueue.main.async { self.dataSource.apply(snapshot, animatingDifferences: true) }
    }

    private func setupCollectionView() {
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        collectionView = UICollectionView(frame: view.bounds, collectionViewLayout: layout)
        collectionView.register(cellWithClass: PhotoCell.self)
        collectionView.backgroundColor = .systemBackground

        if let layout = collectionView.collectionViewLayout as? PinterestLayout {
            layout.delegate = self
        }

        view.addSubview(collectionView)
        collectionView.fillToSuperview()
    }

    extension PhotosViewController: PinterestDelegate {
    func collectionView(_ collectionView: UICollectionView, heightForPhotoAtIndexPath indexPath: IndexPath) -> CGFloat {
        return 200
    }
}
4

1 回答 1

0

您必须将您的 NSDiffableDataSource 对象分配给您的 CollectionView 数据源:

collectionView.dataSource = 数据源

于 2021-01-12T18:01:44.843 回答