Sch*_*eln 6 xcode collectionview swift diffabledatasource
我目前正在开发一个简单的 IOS 应用程序,使用集合视图和可比较数据源。
我初始化视图控制器配置中的所有内容,并在每次更新源数据时调用 reloadView 函数。
当我调用 reloadView 函数时,应用程序崩溃并出现以下错误。但前提是集合视图之前为空。如果那里已经有项目,一切都会正常工作。
Assertion failure in -[_UICollectionCompositionalLayoutSolver _queryClientForSectionDefintionForSectionIndex:]
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid section definition. Please specify a valid section definition when content is to be rendered for a section. This is a client error.'
Run Code Online (Sandbox Code Playgroud)
这就是我的代码的样子:
private func configureHierarchy() {
let layout = collectionViewHelper.createLayout(structure: self.structure)
collectionView = UICollectionView(frame: view.bounds, collectionViewLayout: layout)
collectionView.delegate = self
collectionView.register(RecentCollectionViewCell.self, forCellWithReuseIdentifier: RecentCollectionViewCell.reuseIdentifier)
collectionView.register(OverviewCollectionViewCell.self, forCellWithReuseIdentifier: OverviewCollectionViewCell.reuseIdentifier)
collectionView.register(MessageItemCollectionViewCell.self, forCellWithReuseIdentifier: MessageItemCollectionViewCell.reuseIdentifier)
view.addSubview(collectionView)
}
private func configureDataSource() {
dataSource = UICollectionViewDiffableDataSource<SectionType, Int>(collectionView: collectionView) {
(collectionView: UICollectionView, indexPath: IndexPath, cellIndex: Int) -> UICollectionViewCell? in
let sectionItem = self.structure[indexPath.section]
switch sectionItem.type{
//Returns a collection view cell of the specific type
case .recent:
return getRecentItemCell(...)
case .overview:
return getOverviewItemCell(...)
case .message:
return getMessageItemCell(...)
default:
return nil
}
}
}
Run Code Online (Sandbox Code Playgroud)
我像这样应用快照
private func applySnapshot(structure:[SectionItem]) {
var snapshot = NSDiffableDataSourceSnapshot<SectionType, Int>()
var itemOffset = 0
for structurItem in structure {
snapshot.appendSections([structurItem.type])
snapshot.appendItems(Array(itemOffset..<itemOffset + structurItem.items))
itemOffset += structurItem.items
}
dataSource.apply(snapshot, animatingDifferences: false)
}
Run Code Online (Sandbox Code Playgroud)
我在配置中执行此操作一次
structure = createStructure()
configureHierarchy()
configureDataSource()
applySnapshot(structure: createStructure())
Run Code Online (Sandbox Code Playgroud)
这是我每次数据变化时调用的重新加载函数(如果之前没有数据显示,则会抛出错误)
func reloadView() {
structure = createStructure()
applySnapshot(structure: structure)
}
Run Code Online (Sandbox Code Playgroud)
有什么想法吗?已经非常感谢了!
这是整数_UICollectionCompositionalLayoutSolver!它与您的可比较数据源无关。您可能尚未完成可组合布局的实现。这就是我刚刚发生的事情:) 这恰好是触发异常的地方。
| 归档时间: |
|
| 查看次数: |
4444 次 |
| 最近记录: |