Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Flutter and app store releases

Dequeuing a custom UICollectionViewCell crashes or returns a blank cell

ErrorApp crashes when the collection view asks for its first cell

Short answer

dequeueReusableCell only works for an identifier you registered first. Call register(_:forCellWithReuseIdentifier:) once, with the cell's class for code-built cells or its UINib for cells designed in a nib. With the newer API, create the CellRegistration once, outside the cell provider closure.

  • Applies to Xcode 26, Swift 6, iOS 15 to iOS 26
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Find every place the identifier string is used. It must be registered once before the first dequeue, and the same string must be used on both calls.

    grep -rn "forCellWithReuseIdentifier\|dequeueReusableCell\|CellRegistration" --include=*.swift .
  2. 2Match what you see, then jump to its fix

    1. 1Crash on the first dequeue, with a custom cell that is not a storyboard prototypeNo class or nib is registered for the identifierFix
    2. 2The cell shows, but it is empty or its IBOutlets are nilA class was registered, but the cell is designed in a nibFix
    3. 3Exception on iOS 15 and later, with a diffable data source and dequeueConfiguredReusableCellThe CellRegistration is created inside the cell providerFix

What does the error look like?

  • App crashes when the collection view asks for its first cell
  • Custom cell appears blank, or its outlets are nil
  • Exception when a CellRegistration is created inside a diffable data source cell provider (iOS 15 and later)

Cause 1 of 3

No class or nib is registered for the identifier

1Check

Apple documents that you must register a class or nib for the identifier before calling dequeueReusableCell(withReuseIdentifier:for:). Search for the register call and confirm it runs before the data source is asked for cells, for example in viewDidLoad.

grep -rn "register(" --include=*.swift .

2Fix

Register the cell type once, right after you create the collection view.

collectionView.register(PhotoCell.self,
                        forCellWithReuseIdentifier: PhotoCell.reuseID)

3Verify

Run the screen. The first dequeue returns a cell instead of crashing.

xcodebuild -scheme MyApp -destination 'generic/platform=iOS Simulator' build

Confirmed by developer.apple.com

Did this fix it?

Cause 2 of 3

A class was registered, but the cell is designed in a nib

1Check

A registered class is created with init(frame:), so nothing in the .xib is loaded. Only a registered nib loads the cell from the nib file.

ls PhotoCell.xib && grep -n "register(" PhotoCell*.swift *Controller.swift

2Fix

Register the UINib instead of the class.

let nib = UINib(nibName: "PhotoCell", bundle: nil)
collectionView.register(nib, forCellWithReuseIdentifier: PhotoCell.reuseID)

3Verify

Set a breakpoint in awakeFromNib of the cell. It is hit when the cell is dequeued.

grep -n "awakeFromNib" PhotoCell.swift

Confirmed by developer.apple.com

Did this fix it?

Cause 3 of 3

The CellRegistration is created inside the cell provider

1Check

Apple warns against creating a cell registration inside a UICollectionViewDiffableDataSource cell provider closure. Doing it there prevents reuse and raises an exception on iOS 15 and later.

grep -n -B3 -A3 "CellRegistration" *.swift

2Fix

Create the registration once, as a local constant or property outside the closure, and only pass it in inside the closure. You then do not call register(_:forCellWithReuseIdentifier:) at all.

let cellRegistration = UICollectionView.CellRegistration<UICollectionViewListCell, Int> { cell, indexPath, item in
    var content = cell.defaultContentConfiguration()
    content.text = "\(item)"
    cell.contentConfiguration = content
}

dataSource = UICollectionViewDiffableDataSource<Section, Int>(collectionView: collectionView) {
    collectionView, indexPath, item in
    collectionView.dequeueConfiguredReusableCell(using: cellRegistration, for: indexPath, item: item)
}

3Verify

Scroll the list. Cells are reused and no exception is raised.

grep -n "dequeueConfiguredReusableCell" *.swift

Confirmed by developer.apple.com

Did this fix it?

How do I stop it coming back?

  • Keep each reuse identifier in one constant on the cell class, so the register call and the dequeue call cannot drift apart.
  • Prefer CellRegistration for new code, since it removes the identifier string altogether.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Mobile apps

InstanceID token() no longer works in a Swift Firebase app

Instance ID calls fail to compile after upgrading Firebase to 8.0.0 or later

Firebase removed the Instance ID dependency from Messaging in 8.0.0, so InstanceID.instanceID() calls no longer belong in a current app.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.