我的代码在iOS 11上工作正常,但它偶尔会在iOS 12上崩溃。我得到的错误是:
断言失败 - [UICollectionViewData validateLayoutInRect:]。由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'UICollectionView接收的索引路径不存在的单元格的布局属性:{length = 2,path = 1 - 0}
我有一个显示几个部分的集合视图。当我从服务器获取新数据时,我重新加载其中一个部分。我用来重新加载该部分的代码是:
let attributes = calculateAttributesFrom(items)
layout.firstSectionAttributes = attributes
collectionView.reloadSections([kFirstSectionIndex])
部分中的项目数为:
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if indexPath.section == kFirstSectionIndex {
return layout.firstSectionAttributes.count
}
...
}
我使用layout.firstSectionAttributes.count
作为部分中的项目数。这意味着每个属性必须具有相应的单元格。我想这与layout.invalidateLayout()
有关。我试图在重新加载之前和之后添加它,但它没有帮助。
有谁有想法吗?
用reloadSections
替换reloadData
之后它不再崩溃。但我只想重新加载一个部分而不是所有部分。
试试这个:
collectionView.reloadSections(IndexSet(integer: kFirstSectionIndex))