ios - Crash number of items in section 0 when there are only 0 sections in the collection view
问题描述
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
}
}
解决方案
您必须将您的 NSDiffableDataSource 对象分配给您的 CollectionView 数据源:
collectionView.dataSource = 数据源
推荐阅读
- nosql - Oracle NoSQL 数据库 SQL 查询是否支持数组切片?
- python - 使用 Visual Studio 代码在 GPU 中运行 PyTorch
- xcode - 未定义符号:_OBJC_CLASS_$_RLMObjectBase Xcode 12.4
- reactjs - 如何通过 ReactJS 中的功能组件正确传递数据?
- boost - Boost Spirit 结构 boost::fusion::extension::adt_attribute_proxy 中没有类型名称“value_type”
- go - Unmarshal YAML 文件 - 如何读取带有前导零的 YAML 参数作为字符串?
- regex - Azure 数据工厂派生列 RegexReplace
- javascript - JavaScript - Firebase 实时数据库在网络错误的情况下等待太久
- linux - 根据日期查找HDFS目录下文件的总容量
- python - 如何解决关系“django_session”在具有firebase后端并部署在heroku上的django应用程序中不存在错误