Loading a Reusable UITableViewCell from a Nib
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Nib-backed table view cells are a practical middle ground between storyboard-only layouts and fully code-driven UI. The reusable pattern is straightforward once the nib is registered correctly, the reuse identifier is stable, and the cell class owns its own configuration logic.
Create A Cell Class That Matches The Nib
Start with a custom UITableViewCell subclass and a nib whose root object is a table view cell. The nib name, class name, and reuse identifier do not have to be identical, but keeping them aligned reduces mistakes.
In Interface Builder, set the root view to a table view cell, assign the custom class to ProfileCell, and use a reuse identifier such as ProfileCell. Connect the outlets from the nib to the cell subclass.
Register The Nib Before Dequeuing
The table view does not know about your nib automatically. Register it once, usually in viewDidLoad, before any call to dequeueReusableCell.
This is the core reusable workflow. The table view manages creation and recycling, while your code only supplies row data.
Keep Setup Code In The Cell
A nib-backed cell should expose a small public API, often just one configure(...) method. Styling and one-time visual setup belong inside the cell rather than in the view controller.
If the cell contains transient or asynchronous state, reset it in prepareForReuse():
That prevents recycled cells from showing stale content while new data is loading.
Consider Bundle Selection In Reusable Modules
bundle: nil works when the nib lives in the main app target. If the cell comes from a framework or shared UI module, load the nib from the bundle that owns the class.
That detail becomes important when reusable UI components are packaged outside the main application target.
Know When A Nib Is The Right Choice
Nib-backed cells are great when visual editing speeds up layout work and the cell shape is reasonably stable. For extremely simple rows, a code-only cell may be faster to maintain. For highly dynamic view hierarchies shared across many contexts, programmatic layout can scale better.
The nib approach is strongest when you want Interface Builder convenience without giving up table view reuse.
Common Pitfalls
- Forgetting to register the nib before dequeuing the cell.
- Using different strings for the nib name and reuse identifier without keeping them in sync.
- Connecting outlets to the wrong object inside Interface Builder.
- Putting cell-specific rendering logic in the view controller instead of the cell class.
- Loading the nib from the wrong bundle when the cell lives in a framework.
Summary
- Create a custom
UITableViewCellsubclass and a matching nib. - Register the nib with the table view before calling
dequeueReusableCell. - Keep content binding in a
configure(...)method and cleanup inprepareForReuse(). - Use the correct bundle when the nib is packaged outside the main app target.
Related reading
- Loading an overlay when running long tasks in iOS
- Loading/Downloading image from URL on Swift
- Loading/Downloading image from URL on Swift
- Localizable.strings - The data couldn’t be read because it isn’t in the correct format
- Location based horizontal scalable dating app database model
- Location Services not working in iOS 11
- LogCat message The Google Play services resources were not found. Check your project configuration to ensure that the resources are included
- Logging with Retrofit 2
.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.