我用CustomLayout在CollectionView中做了一个视图。在iOS6中,它工作得很好,但在iOS7中,它抛出了一个类似这样的异常。
由于未捕获异常'NSInternalInconsistencyException‘而终止应用程序,原因:
辅助项在索引路径({长度= 2,路径=0- 0})的布局属性从CustomSupplementaryAttributes: 0xd1123a0索引路径:(NSIndexPath: 0xd112580 {长度= 2,路径=0- 0});元素类型:(标识符);frame = (0 0;1135.66 45);zIndex = -1;到CustomSupplementaryAttributes: 0xd583c80索引路径:(NSIndexPath: 0xd583c70 {长度= 2,路径=0- 0});元素类型:(标识符);frame = (0 0;1135.66 45);zIndex = -1;而不会使布局的无效
发布于 2013-10-06 19:29:25
在更新之前,您需要使现有布局无效,请参阅错误消息的结尾:
而不使布局无效‘
[collectionViewLayout invalidateLayout];发布于 2016-06-01 16:29:23
iOS 10
在iOS 10中,引入了一个新特性,即Cell Prefetching。它会让SupplementaryView的动态定位崩溃。为了在旧的行为中运行,它需要禁用prefetchingEnabled。在iOS 10中默认为true。
// Obj-C
// This function is available in iOS 10. Disable it for dynamic position of `SupplementaryView `.
if ([self.collectionView respondsToSelector:@selector(setPrefetchingEnabled:)]) {
self.collectionView.prefetchingEnabled = false;
}
// Swift
if #available(iOS 10, *) {
// Thanks @maksa
collectionView.prefetchingEnabled = false
// Swift 3 style
colView.isPrefetchingEnabled = false
}我讨厌这个问题。我花了两天时间来解决这个问题。关于Cell Pre-fetch @iOS 10的参考资料。
iOS 9及更早版本...
@Away Lin is right.。我通过实现这个委托方法解决了同样的问题。
我的Custom UICollectionViewLayout将修改layoutAttributesForElementsInRect中的属性。截面位置是动态的,而不是静态的。因此,我得到了关于layout attributes for supplementary item at index path ... changed from ... to ...的警告。在更改之前,应该调用与invalideLayout相关的方法。
而且,在实现此委托方法以返回true之后,在滚动UICollectionViewLayout时将调用方法invalidateLayoutWithContext:。默认情况下,它返回false。
- (BOOL) shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds {
return YES;
}如果集合视图需要布局更新,则
返回值true;如果不需要更改布局,则返回值false。
讨论此方法的默认实现返回false。子类可以覆盖它,并根据集合视图边界的更改是否需要更改单元格和补充视图的布局来返回适当的值。
如果集合视图的边界更改并且此方法返回true,则集合视图通过调用invalidateLayoutWithContext:方法使布局无效。
可用性在iOS 6.0及更高版本中可用。
还有更多。
A nice example project on GitHub, for custom UICollectionViewLayout.
发布于 2013-10-10 06:31:00
我也有同样的例外:在iOS 7中,您现在需要重写UICollectionViewLayoutAttributes子类中继承的isEqual:,如苹果文档here中所述。
https://stackoverflow.com/questions/19207924
复制相似问题