Dequeuing a custom UICollectionViewCell crashes or returns a blank cell
App crashes when the collection view asks for its first cellShort 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.
Fix it fast
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 .2Match what you see, then jump to its fix
- 1
Crash on the first dequeue, with a custom cell that is not a storyboard prototypeNo class or nib is registered for the identifierFix - 2
The cell shows, but it is empty or its IBOutlets are nilA class was registered, but the cell is designed in a nibFix - 3
Exception on iOS 15 and later, with a diffable data source and dequeueConfiguredReusableCellThe CellRegistration is created inside the cell providerFix
- 1
What does the error look like?
App crashes when the collection view asks for its first cellCustom cell appears blank, or its outlets are nilException 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' buildConfirmed by developer.apple.com
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.swift2Fix
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.swiftConfirmed by developer.apple.com
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" *.swift2Fix
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" *.swiftConfirmed by developer.apple.com
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
UserDefaults logs 'Failed to read values in CFPrefsPlistSource' for an app group
Settings saved by the app are not visible to its extension, widget or sibling app that share the group
This log comes from the preferences system when UserDefaults is opened for an app group's suite.
3 causes, each with a fixMobile apps
iOS app crashes: Google Maps SDK must be initialized via provideAPIKey
Google Maps SDK for iOS must be initialized via [GMSServices provideAPIKey:...] prior to use
The Maps SDK throws this when a Maps object is created before GMSServices.provideAPIKey has run.
3 causes, each with a fixMobile apps
UIKit app crashes because it cannot find the storyboard
Could not find a storyboard named 'Main' in bundle NSBundle
UIStoryboard throws this exception when no storyboard file with that name exists in the bundle it searches.
3 causes, each with a fixMobile apps
Firebase on iOS asks for the FCM token before APNs has given one
Be sure to re-retrieve the FCM token once the APNS device token is set.
The Firebase Messaging SDK needs the APNs device token from iOS before it hands out an FCM token.
3 causes, each with a fixMobile apps
A test push from the Firebase console never shows up on the iPhone
didReceiveRegistrationToken prints a token, yet no notification arrives
When a console message is accepted but the iPhone stays silent, the break is between Firebase and APNs or inside the app.
4 causes, each with a fixMobile 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