When to use dequeueReusableCellWithIdentifier vs dequeueReusableCellWithIdentifier forIndexPath
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
These two dequeue APIs reflect two generations of table-view usage in UIKit. In modern iOS code, dequeueReusableCell(withIdentifier:for:) is the standard choice because it guarantees a cell when registration is set up correctly, while the older dequeueReusableCell(withIdentifier:) returns an optional and belongs mostly to legacy patterns.
The older API
The older method is:
This may return nil if no reusable cell exists and no registration or storyboard prototype covers that identifier. Older code often looked like this:
That was normal when cell creation was managed manually.
The modern API
The newer and preferred method is:
This method assumes you have already registered the cell class or nib, or provided a prototype cell in Interface Builder. If registration is correct, the method always returns a cell and does not require a nil check.
Typical setup:
Then in cellForRowAt:
This is cleaner and matches how Apple expects table views to be configured in current UIKit code.
Why the for: version is better
Advantages of dequeueReusableCell(withIdentifier:for:):
- no optional handling in normal use
- cleaner
cellForRowAtimplementation - stronger alignment with registration-based cell setup
- fewer hidden fallback creation paths
If registration is missing, it fails loudly, which is usually better than returning nil and hiding a setup mistake.
When the old method still appears
You still see dequeueReusableCell(withIdentifier:) in:
- older Objective-C and Swift codebases
- highly customized legacy table setup
- code paths outside
cellForRowAtwhere no index path is available
Even then, it is often a signal that the view code predates the more modern registration pattern.
UICollectionView follows the same direction
The same general idea exists for collection views. Modern UIKit encourages registration and index-path-based dequeuing because it makes reuse behavior more explicit and predictable.
That is why, in current code, the question is usually not "which one should I prefer," but "is there a specific legacy reason I still need the old one."
If there is no such reason, the for: variant should usually be your default starting point.
Common Pitfalls
The most common mistake is using dequeueReusableCell(withIdentifier:for:) without registering the cell class, nib, or storyboard prototype first. Another is continuing to use the old optional-returning method in new code simply out of habit from older tutorials. Developers also sometimes create fallback cells manually while also trying to use registration-based reuse, which mixes two patterns unnecessarily. Confusion can also arise when the older method is called outside cellForRowAt, because the lack of an index path changes what the API can guarantee. Finally, people often focus on dequeuing method choice while forgetting the more important requirement: reset reused cell state correctly during configuration.
Summary
- In modern UIKit, prefer
dequeueReusableCell(withIdentifier:for:). - The
for:version assumes proper registration and returns a non-optional cell. - The older
dequeueReusableCell(withIdentifier:)mainly survives in legacy or specialized code. - Registration setup is the key requirement for the newer API to work correctly.
- Use one reuse pattern consistently instead of mixing manual creation with registered dequeuing.
- Focus on correct cell configuration after dequeuing, because reuse bugs often come from stale state rather than the dequeue call itself.
Related reading
- When to use objc in Swift?
- When to use PNG or JPG in iPhone development?
- When to use RxJava in Android and when to use LiveData from Android Architectural Components?
- When to use Storyboard and when to use XIBs
- When to use UICollectionView instead of UITableView?
- Where and how to populate my tab using fragment
- Where are iOS simulator screenshots stored?
- Where can I find Android source code online?
.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.