滚动和拖动单元格时集合视图抛出异常

Posted

技术标签:

【中文标题】滚动和拖动单元格时集合视图抛出异常【英文标题】:Collection view throws exception when scrolling and dragging a cell 【发布时间】:2022-01-19 19:30:25 【问题描述】:

我的应用显示一个带有一些卡片的板,尝试在列之间实现 D&D 卡片。

为了实现它使用水平滚动的集合视图,集合视图单元格代表列。

每一列是另一个垂直滚动的集合视图,单元格是卡片。

我可以在可见列之间进行拖放而没有问题,但是如果我试图将卡片拖到视图之外的列,视图将滚动并且我可以看到该列,但是在拖放时出现异常:

*** Assertion failure in -[UICollectionView _validateScrollingTargetIndexPath:], UICollectionView.m:6485
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Attempted to scroll the collection view to an out-of-bounds item (0) when there are only 0 items in section 0.
*** First throw call stack:
(
    0   CoreFoundation                      0x00007fff203fbbb4 __exceptionPreprocess + 242
    1   libobjc.A.dylib                     0x00007fff2019ebe7 objc_exception_throw + 48
    2   Foundation                          0x00007fff20750c12 _userInfoForFileAndLine + 0
    3   UIKitCore                           0x00007fff2465de4b -[UICollectionView _validateScrollingTargetIndexPath:] + 405
    4   UIKitCore                           0x00007fff2465e290 -[UICollectionView _contentOffsetForScrollingToItemAtIndexPath:atScrollPosition:] + 52
    5   UIKitCore                           0x00007fff2465ef51 -[UICollectionView _scrollToItemAtIndexPath:atScrollPosition:animated:] + 166
    6   UIKitCore                           0x00007fff246d1593 -[_UICollectionViewDragDestinationController _reorderingDisplayLinkDidTick] + 1800
    7   QuartzCore                          0x00007fff287c80a4 _ZN2CA7Display11DisplayLink14dispatch_itemsEyyy + 914
    8   QuartzCore                          0x00007fff288bf828 _ZL22display_timer_callbackP12__CFMachPortPvlS1_ + 393
    9   CoreFoundation                      0x00007fff2033670d __CFMachPortPerform + 157
    10  CoreFoundation                      0x00007fff2036a6f1 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 41
    11  CoreFoundation                      0x00007fff20369abe __CFRunLoopDoSource1 + 607
    12  CoreFoundation                      0x00007fff20363ff8 __CFRunLoopRun + 2578
    13  CoreFoundation                      0x00007fff20363103 CFRunLoopRunSpecific + 567
    14  GraphicsServices                    0x00007fff2c851cd3 GSEventRunModal + 139
    15  UIKitCore                           0x00007fff24ffbe63 -[UIApplication _run] + 928
    16  UIKitCore                           0x00007fff25000a53 UIApplicationMain + 101

有什么问题?我该如何解决?

更新:

我试图清除 collectionView 并重新加载 prepareForReuse 上的数据。这修复了上述异常。但是我收到了关于在拖放交互过程中清除 collectionView.dropDelegate 的警告,并且单元格的行为不正确。

真正对我有用的是不为具有不同索引的板列重用单元格。所以我为每个索引路径使用不同的标识符注册列的单元格(通常板有 3 到 5 列)。

【问题讨论】:

【参考方案1】:

我能够通过实现 prepareForReuse 方法并重置 collectionView 来解决此问题。

    override func prepareForReuse() 
        super.prepareForReuse()
        collectionView.dataSource = nil
        collectionView.delegate = nil
        collectionView.dragDelegate = nil
        collectionView.dropDelegate = nil
        collectionView.reloadData()
    

【讨论】:

以上是关于滚动和拖动单元格时集合视图抛出异常的主要内容,如果未能解决你的问题,请参考以下文章

随机“CALayerInvalidGeometry原因:CALayer位置包含NaN”异常

在我的表格视图中设置单元格时索引超出范围

当作为子视图添加到集合视图单元格时,滚动视图中的内容不可见

IOS UICollectionView 在使用两个集合视图时抛出断言

UICollectionView 使用自定义布局抛出未捕获的异常

字典方法 Remove 和 Clear (.NET Core) 在枚举期间修改集合。没有抛出异常