Sen*_*ful 5 ios uicollectionview swift
我正在使用UICollectionView的performBatchUpdates(_:completion:)方法.问题是,有时我的复杂差异逻辑会失败并返回不正确数量的要插入的部分.这会导致我插入的项目数与从数据源报告的数量不匹配.每当发生这种情况时,我们会收到以下错误:
断言失败 - [CollectionView _endItemAnimationsWithInvalidationContext:tentativelyForReordering:animator:]
执行批量更新时出错:无效更新:无效的节数.更新后的集合视图中包含的节数(25)必须等于更新前的集合视图中包含的节数(19),加上或减去插入或删除的节数(插入0,0删除).
我意识到正确的解决方案是修复我的差异逻辑,使得返回的项目数和我调用的插入数之间没有不匹配.
但是,我想做的是使它即使我的逻辑在将来失败,而不是碰撞应用程序,它只会重新加载集合视图的数据.
我怎么能在Swift中做到这一点?
默认情况下,Swift 中无法捕获 NSExceptions,因此创建一个类似于this one或this one的桥。
即使您发现断言失败,用户也无法与集合视图交互,因此您需要重新创建集合视图。
TryCatch.try({
collectionView.performBatchUpdates({
collectionView.insertItems(at: indexPaths)
collectionView.insertSections(sections)
}, completion: nil)
}, catch: { exception in
print("Error updating collection view: \(exception)")
collectionView.removeFromSuperview()
// recreate the collection view (make sure to set datasource and delegates)
collectionView = ...
collectionView.dataSource = ...
collectionView.delegate = ...
}, finally: nil)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3616 次 |
| 最近记录: |