使用自定义布局重新加载 UICollectionView 会引发错误
Reloading UICollectionView with custom layout throws error
我正在尝试使用此 tutorial 在我的 UICollectionView 上实现 Apple Watch 动画。
它有一个单独的 class of UICollectionViewLayout 用于所需的布局。
我将我的元素放在数组中,当我必须用我以前调用的新值更新集合视图时
self.collectionView.reloadData()
但它抛出我以下错误
*** Assertion failure in -[UICollectionViewData validateLayoutInRect:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit/UIKit-3600.8.1/UICollectionViewData.m:433
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UICollectionView received layout attributes for a cell with an index path that does not exist: <NSIndexPath: 0x170222520> {length = 2, path = 0 - 1}'
然后我用了
self.collectionView.collectionViewLayout.invalidateLayout()
在重新加载之前使布局无效。但是我的代码仍然抛出之前的错误。所以我尝试重新加载部分:
let indexSet = IndexSet(integer: 0)
self.collectionView.reloadSections(indexSet)
这次有时布局显示不正确。它有一个扭曲的 UI,有时它会在以下代码行中抛出 EXC_BAD_ACCESS 错误:
self.collectionView.collectionViewLayout = layout // error comes on this line
有什么解决办法吗?请查看上面提到的教程以了解我的代码。我参考了很多答案,但我无法解决这个问题。
问题是因为即使数据为 nil,您也会返回 UICollectionViewLayoutAttributes,因此出现错误:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UICollectionView received layout attributes for a cell with an index path that does not exist: <NSIndexPath: 0x170222520> {length = 2, path = 0 - 1}'
在 tutorial 中,从 layoutAttributesForElementsInRect
调用 layoutAttributesForItemAtIndexPath
属性的方法 layoutAttributesForItemAtIndexPath
,其中 cellCount
是 [=14] =]
假设您可能更改了 numberOfItemsInSection
以反映您的数据,您也必须更改 cellCount
的条件。
我正在尝试使用此 tutorial 在我的 UICollectionView 上实现 Apple Watch 动画。 它有一个单独的 class of UICollectionViewLayout 用于所需的布局。
我将我的元素放在数组中,当我必须用我以前调用的新值更新集合视图时
self.collectionView.reloadData()
但它抛出我以下错误
*** Assertion failure in -[UICollectionViewData validateLayoutInRect:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit/UIKit-3600.8.1/UICollectionViewData.m:433
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UICollectionView received layout attributes for a cell with an index path that does not exist: <NSIndexPath: 0x170222520> {length = 2, path = 0 - 1}'
然后我用了
self.collectionView.collectionViewLayout.invalidateLayout()
在重新加载之前使布局无效。但是我的代码仍然抛出之前的错误。所以我尝试重新加载部分:
let indexSet = IndexSet(integer: 0)
self.collectionView.reloadSections(indexSet)
这次有时布局显示不正确。它有一个扭曲的 UI,有时它会在以下代码行中抛出 EXC_BAD_ACCESS 错误:
self.collectionView.collectionViewLayout = layout // error comes on this line
有什么解决办法吗?请查看上面提到的教程以了解我的代码。我参考了很多答案,但我无法解决这个问题。
问题是因为即使数据为 nil,您也会返回 UICollectionViewLayoutAttributes,因此出现错误:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UICollectionView received layout attributes for a cell with an index path that does not exist: <NSIndexPath: 0x170222520> {length = 2, path = 0 - 1}'
在 tutorial 中,从 layoutAttributesForElementsInRect
调用 layoutAttributesForItemAtIndexPath
属性的方法 layoutAttributesForItemAtIndexPath
,其中 cellCount
是 [=14] =]
假设您可能更改了 numberOfItemsInSection
以反映您的数据,您也必须更改 cellCount
的条件。