ios - NSInternalInconsistencyException - “请求 MSCollectionElementKindCurrentTimeHorizo​​ntalGridline 装饰 View 的布局属性

标签 ios objective-c uicollectionview uicollectionviewlayout

我处理的应用程序正在以日历的形式显示 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 currentTimeIndicatorAttributescurrentTimeHorizo​​ntalGridlineAttributes

要修复该错误,我只需使用以下内容扩展 invalidateLayoutCache 方法:

[self.currentTimeIndicatorAttributes removeAllObjects];
[self.currentTimeHorizontalGridlineAttributes removeAllObjects];

关于ios - NSInternalInconsistencyException - “请求 MSCollectionElementKindCurrentTimeHorizo​​ntalGridline 装饰 View 的布局属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25568585/

相关文章:

ios - Objective-C @available 守卫与更多条件

ios - UIKit Collection View ,奇怪的行为(访问/滚动单元格)

iphone - iOS 设备上的核心日期获取请求

ios - 在 Swift 的 UITableView 中搜索时禁用 scrollViewDidScroll 方法

ios - 如何使用 AVAssetImageGenerator 立即获取远程视频的低质量缩略图

ios - swift 。按 n 个元素加载 collectionView

ios - UITableView 和 UICollectionView 在一行中重用第一行中的集合

ios - 无法在 iOS 模拟器中运行 flutter 应用程序 : warning: [CP] Unable to find matching . xcframework slice in ... [Flutter]

ios - 在 Interface Builder 中从 "first"重命名选项卡

objective-c - 如何设置 UISegmentedControl 的默认状态?