skip to Main Content

Using Xcode 16 beta, I’m now getting the following runtime error.

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Expected dequeued view to be returned to the collection view in preparation for display. When the collection view's data source is asked to provide a view for a given index path, ensure that a single view is dequeued and returned to the collection view. Avoid dequeuing views without a request from the collection view. For retrieving an existing view in the collection view, use -[UICollectionView cellForItemAtIndexPath:] or -[UICollectionView supplementaryViewForElementKind:atIndexPath:].

It gets thrown on my

@UIApplicationMain
class AppDelegate

which is making it difficult to locate the actual offending code.

The error message does say that the Dequeued View is a custom class that extends UICollectionViewCell:
Dequeued view: <MyProject.MyCollectionViewCell: 0x15393d780; baseClass = UICollectionViewCell;

At this point, I’d like to be able to figure out where the actual dequeue is that’s causing this? Or is it possible its not in the dequeue?

Full Stack Trace

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Expected dequeued view to be returned to the collection view in preparation for display. When the collection view's data source is asked to provide a view for a given index path, ensure that a single view is dequeued and returned to the collection view. Avoid dequeuing views without a request from the collection view. For retrieving an existing view in the collection view, use -[UICollectionView cellForItemAtIndexPath:] or -[UICollectionView supplementaryViewForElementKind:atIndexPath:]. Dequeued view: <MyProject.MyCollectionViewCell: 0x15393d780; baseClass = UICollectionViewCell; frame = (34 35; 325 90); opaque = NO; backgroundColor = UIExtendedGrayColorSpace 0 0; layer = <CALayer: 0x6000045af9a0>>; Collection view: <UICollectionView: 0x11ddf1e00; frame = (0 0; 393 582.333); clipsToBounds = YES; autoresize = RM+BM; gestureRecognizers = <NSArray: 0x600000de1920>; backgroundColor = <UIDynamicCatalogColor: 0x60000219acb0; name = sptBackground>; layer = <CALayer: 0x6000007510a0>; contentOffset: {0, 0}; contentSize: {393, 159}; adjustedContentInset: {0, 0, 0, 0}; layout: <UICollectionViewFlowLayout: 0x1042a4c70>; dataSource: <MyProject.MyCollectionsViewController: 0x11dde5200>>'
*** First throw call stack:
(
    0   CoreFoundation                      0x00000001804b70ec __exceptionPreprocess + 172
    1   libobjc.A.dylib                     0x000000018008ede8 objc_exception_throw + 72
    2   Foundation                          0x0000000180e73aa8 _userInfoForFileAndLine + 0
    3   UIKitCore                           0x00000001851ace44 __43-[UICollectionView _updateVisibleCellsNow:]_block_invoke.444 + 136
    4   UIKitCore                           0x0000000185a9811c -[_UICollectionViewSubviewManager removeAllDequeuedViewsWithEnumerator:] + 188
    5   UIKitCore                           0x00000001851ac90c -[UICollectionView _updateVisibleCellsNow:] + 4000
    6   UIKitCore                           0x00000001851b1714 -[UICollectionView layoutSubviews] + 284
    7   UIKitCore                           0x0000000186016438 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 2404
    8   QuartzCore                          0x000000018b059eb0 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 432
    9   UIKitCore                           0x000000018600583c -[UIView(Hierarchy) layoutBelowIfNeeded] + 308
    10  UIKitCore                           0x000000018529304c -[UINavigationController _layoutViewController:] + 812
    11  UIKitCore                           0x000000018528cac4 -[UINavigationController _layoutTopViewControllerLookForNested:] + 416
    12  UIKitCore                           0x000000018528b004 -[UINavigationController _didEndTransitionFromView:toView:wasCustom:] + 820
    13  UIKitCore                           0x0000000185c31908 -[_UIViewControllerTransitionConductor _startTransition:fromViewController:toViewController:] + 1004
    14  UIKitCore                           0x0000000185c311b0 -[_UIViewControllerTransitionConductor startDeferredTransitionIfNeeded] + 640
    15  UIKitCore                           0x0000000185293d4c -[UINavigationController __viewWillLayoutSubviews] + 80
    16  UIKitCore                           0x000000018527bb00 -[UILayoutContainerView layoutSubviews] + 168
    17  UIKitCore                           0x0000000186016438 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 2404
    18  QuartzCore                          0x000000018b059eb0 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 432
    19  QuartzCore                          0x000000018b064c34 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 124
    20  QuartzCore                          0x000000018af99c58 _ZN2CA7Context18commit_transactionEPNS_11TransactionEdPd + 464
    21  QuartzCore                          0x000000018afc8468 _ZN2CA11Transaction6commitEv + 652
    22  UIKitCore                           0x0000000185ab8260 __34-[UIApplication _firstCommitBlock]_block_invoke_2 + 32
    23  CoreFoundation                      0x000000018041b0ec __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 20
    24  CoreFoundation                      0x000000018041a824 

__CFRunLoopDoBlocks + 352
    25  CoreFoundation                      0x00000001804150c8 __CFRunLoopRun + 812
    26  CoreFoundation                      0x0000000180414960 CFRunLoopRunSpecific + 536
    27  GraphicsServices                    0x000000019016fb10 GSEventRunModal + 160
    28  UIKitCore                           0x0000000185a9f650 -[UIApplication _run] + 796
    29  UIKitCore                           0x0000000185aa3848 UIApplicationMain + 124
    30  MyProject.debug.dylib          0x000000010983da34 __debug_main_executable_dylib_entry_point + 64
    31  dyld                                0x000000010298d410 start_sim + 20
    32  ???                                 0x0000000102b92154 0x0 + 4340654420
    33  ???                                 0x352f000000000000 0x0 + 3832281807915581440
)
libc++abi: terminating due to uncaught exception of type NSException

UPDATE

I have narrowed down the issue to this block of code in cellForItemAt:


let cell = collectionView.dequeueReusableCell(withReuseIdentifier:item.reuseIdentifier(), for: indexPath) as UICollectionViewCell

 switch cell {
 case is CollectionViewCell:

if let customCell = cell as? CustomCollectionViewCell {
    
    if Device.isPhone && (item is FirstTypeItem || item is SecondTypeItem) {
        
        guard let phoneCell = collectionView.dequeueReusableCell(withReuseIdentifier: "PhoneCell", for: indexPath) as? PhoneCollectionCell else {
            return cell // Return the original cell if casting fails
        }
        
        phoneCell.setSomeValue(value)
        return phoneCell
    }
    
    customCell.setSomeValue(otherValue)
    return customCell
}

return cell

Once the dequeuReusableCell is called with the "PhoneCell" reuse identifier, I run into the above error.

The "PhoneCell" reuse identifier is not registered. If I register it, then I do not hit the above error, but instead I get nil @IBOutlet’s in the didSet of the PhoneCollectionType cell.

collectionView.register(PhoneCollectionCell.self, forCellWithReuseIdentifier: "PHoneCell")

I think it has to do with dequeuing from the same indexPath twice, with a different reuseIdentifier the second time

2

Answers


  1. Are you using dequeueReusableCell outside of cellForItemAt for cell sizing, for instance?

    I resolved this by replacing calls of dequeueReusableCell outside of cellForItemAt by instantiating cells directly.

    Open the comments in this thread https://forums.developer.apple.com/forums/thread/756645?answerId=790296022#790296022

    Login or Signup to reply.
  2. @Steven – Can you please, show an example how you did resolve that?

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search