【问题标题】:Crash number of items in section 0 when there are only 0 sections in the collection view当集合视图中只有 0 个部分时,第 0 部分中的项目数崩溃
【发布时间】:2020-09-06 12:34:01
【问题描述】:

我想通过 Pinterest Layout 学习使用 UICollectionViewDiffableDataSource,但是当我尝试运行我的模拟器时。它崩溃并给我一条消息

当集合视图中只有 0 个部分时,请求第 0 部分中的项目数

我使用 raywenderlich tutorial 做了 Pinterest 布局,当我在 google 中偶然发现问题的原因是 Pinterest 布局。但是在我使用 diffableDataSource 之前它可以正常工作,但是在使用 diffableDataSource 之后它会崩溃。我哪里做错了?你能帮帮我吗,这是我的 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
    }
}

【问题讨论】:

    标签: ios swift diffabledatasource nsdiffabledatasourcesnapshot uicollectionviewdiffabledatasource


    【解决方案1】:

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

    collectionView.dataSource = 数据源

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多