我处理的应用程序正在以日历的形式显示 CollectionView。 为了简化,我们使用了 MSCollectionViewCalendarLayout来自github。 现在它带有多个额外的装饰 View ,例如一条红线,代表日历中的当前时间。
在我们的实现中,还可以过滤不同的类型,因此日历并不总是显示相同种类/数量的单元格。不幸的是,有时应用程序会莫名其妙地崩溃,而此时它应该显示红色时间线:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'request for layout attributes for decoration view of kind MSCollectionElementKindCurrentTimeHorizontalGridline in section 0 when there are only 0 sections in the collection view'
*** First throw call stack:
(
0 CoreFoundation 0x034761e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x02fcb8e5 objc_exception_throw + 44
2 CoreFoundation 0x03476048 +[NSException raise:format:arguments:] + 136
3 Foundation 0x00e2f4de -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 116
4 UIKit 0x018eb290 -[UICollectionViewData layoutAttributesForDecorationViewOfKind:atIndexPath:] + 228
5 UIKit 0x018b5764 -[UICollectionView _dequeueReusableViewOfKind:withIdentifier:forIndexPath:viewCategory:] + 242
6 UIKit 0x018a8036 -[UICollectionView _createPreparedSupplementaryViewForElementOfKind:atIndexPath:withLayoutAttributes:applyAttributes:] + 381
7 UIKit 0x018a9775 -[UICollectionView _updateVisibleCellsNow:] + 4730
8 UIKit 0x018ad65f -[UICollectionView layoutSubviews] + 265
9 UIKit 0x012d0964 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 355
10 libobjc.A.dylib 0x02fdd82b -[NSObject performSelector:withObject:] + 70
11 QuartzCore 0x026a445a -[CALayer layoutSublayers] + 148
12 QuartzCore 0x02698244 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
13 QuartzCore 0x026980b0 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 26
14 QuartzCore 0x025fe7fa _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 294
15 QuartzCore 0x025ffb85 _ZN2CA11Transaction6commitEv + 393
16 QuartzCore 0x02600258 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 92
17 CoreFoundation 0x0343e36e __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 30
18 CoreFoundation 0x0343e2bf __CFRunLoopDoObservers + 399
19 CoreFoundation 0x0341c254 __CFRunLoopRun + 1076
20 CoreFoundation 0x0341b9d3 CFRunLoopRunSpecific + 467
21 CoreFoundation 0x0341b7eb CFRunLoopRunInMode + 123
22 GraphicsServices 0x049615ee GSEventRunModal + 192
23 GraphicsServices 0x0496142b GSEventRun + 104
24 UIKit 0x01261f9b UIApplicationMain + 1225
25 CariAudit 0x001036ad main + 141
26 libdyld.dylib 0x039d9701 start + 1
27 ??? 0x00000003 0x0 + 3
)
libc++abi.dylib: terminating with uncaught exception of type NSException
现在我知道,使用这个自定义布局,很多人在更改 collectionView 的基础数据时都会收到此错误消息,对此的解决方案是在布局上调用“invalidateLayoutCache”。唯一的问题是我的代码中已经有了它,它在调用 collectionView 上的 reloadData 之前被调用。
有没有人有解决此类问题的想法/方法? 谢谢
最佳答案
经过几个小时的搜索,我终于找到了错误。 问题是 MSCollectionViewCalendarLayout 没有清空 Attributes-Dictionaries currentTimeIndicatorAttributes 和 currentTimeHorizontalGridlineAttributes。
要修复该错误,我只需使用以下内容扩展 invalidateLayoutCache 方法:
[self.currentTimeIndicatorAttributes removeAllObjects];
[self.currentTimeHorizontalGridlineAttributes removeAllObjects];
关于ios - NSInternalInconsistencyException - “请求 MSCollectionElementKindCurrentTimeHorizontalGridline 装饰 View 的布局属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25568585/