UICollectionView's cellForItemAtIndexPath is not being called
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
If collectionView(_:cellForItemAt:) is never called, the collection view is usually not ready to ask for cells yet. The problem is almost always in one of four places: data source wiring, item count, cell registration, or layout and visibility.
Check the Data Source First
The collection view will not request a cell unless it has a data source and that data source reports at least one item.
If numberOfItemsInSection returns 0, cellForItemAt will never be called. That is normal behavior, not a collection-view bug.
Verify Cell Registration and Reuse Identifier
The next thing to check is whether the cell is registered or configured correctly in Interface Builder.
If you register in code:
then your dequeue call must use the exact same identifier:
For storyboard-based cells, confirm that:
- the collection view cell exists in the storyboard
- the reuse identifier matches exactly
- the outlet to the collection view is connected
Layout and Geometry Matter
Even with a valid data source, a collection view may still not ask for visible cells if its geometry is broken.
Common examples:
- the collection view frame is zero
- the item size is zero
- constraints collapse the view offscreen
- the collection view is hidden or covered
A simple diagnostic:
If the frame or content size is zero, the view cannot display cells correctly.
Provide a Real Item Size
If you use UICollectionViewFlowLayout, make sure the item size is not accidentally zero.
Broken sizing logic is one of the fastest ways to end up with a collection view that technically exists but never renders cells.
Async Data Requires a Reload
If your backing data is loaded asynchronously, the collection view must be reloaded after the data arrives and on the main thread.
If reloadData() runs before the model is updated, or runs off the main thread, the visible result can be empty.
A Good Debugging Sequence
When diagnosing this problem, check methods in this order:
viewDidLoadnumberOfItemsInSectioncellForItemAt
If viewDidLoad runs but numberOfItemsInSection does not, the data source is probably not connected.
If numberOfItemsInSection runs and returns a positive count but cellForItemAt still does not, the next suspects are layout, visibility, or registration.
Common Pitfalls
Forgetting to assign collectionView.dataSource is one of the most common causes.
Returning zero items because the model has not loaded yet makes the collection view behave correctly but look broken.
Using mismatched reuse identifiers or failing to register the cell causes dequeue and rendering problems.
Letting Auto Layout or item-size logic collapse the view to zero prevents cells from being requested visibly.
Summary
- '
cellForItemAtis only called when the collection view has a data source, positive item count, and visible layout.' - Always verify
numberOfItemsInSectionbefore chasing more complex explanations. - Check reuse identifiers and cell registration carefully.
- Inspect frame size, content size, and item size when the view appears empty.
- Reload data on the main thread after asynchronous model updates.
Related reading
- UIDevice uniqueIdentifier deprecated - What to do now?
- UIFont - how to get system thin font
- UIGestureRecognizer blocks subview for handling touch events
- UIGestureRecognizer on UIImageView
- UIImagePickerController error Snapshotting a view that has not been rendered results in an empty snapshot in iOS 7
- UIImageView missing images in Launch Screen on device
- UIImage - implementing an auto levels algorithm
- UIImage Resize, then Crop
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.