Handling an empty UITableView. Print a friendly message
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
An empty UITableView should look intentional, not broken. The usual solution is to show a friendly empty-state view when the data source has no rows, then remove that view when content becomes available again.
Use backgroundView for the Empty State
The simplest pattern is to attach a label or custom view to the table view’s backgroundView. This keeps the empty state visually tied to the table and avoids extra layout complexity.
This works well when a single sentence is enough to explain the state.
Update the Empty State When Data Changes
The empty-state logic should run whenever the data changes, not only in viewDidLoad.
Keeping the empty-state decision in one helper method prevents drift between the visual state and the actual data.
Distinguish Empty, Filtered, and Error States
Zero rows do not always mean the same thing. A screen might be empty because:
- the user has no content yet
- a filter removed all matching rows
- loading failed
Those states should not all use the same message. “No messages yet” is very different from “No results match your filter” or “Could not load messages. Pull to refresh.”
You can reuse the same backgroundView mechanism while changing the content according to the reason.
Use a Custom View for Richer Empty States
If a plain label is too limited, use a small stack view or a custom UIView instead.
This gives you space for a title, explanation, and even a retry button if the design calls for one.
Keep the Empty State Accessible
Empty-state text should use Dynamic Type friendly fonts and readable contrast. If the screen is actionable, the message should explain what the user can do next. A good empty state is not just decorative. It reduces confusion.
Consistency matters too. If every list in the app handles emptiness differently, the interface feels accidental. Reusing one small empty-state pattern across list screens improves the product feel immediately.
Common Pitfalls
A common mistake is setting the empty message once and forgetting to refresh it after network responses, filtering, or pull-to-refresh.
Another mistake is leaving separators visible behind the empty state, which makes the table look half-rendered.
It is also easy to blur empty and error states together. When loading fails, say that clearly instead of pretending there is simply no data.
Summary
- Use
tableView.backgroundViewas the simplest empty-state mechanism. - Refresh the empty state every time the underlying data changes.
- Distinguish no-data, no-results, and error scenarios with different messages.
- Use a custom background view when a label is not enough.
- Make the empty state accessible, clear, and intentional.
Related reading
- Handling applicationDidBecomeActive - How can a view controller respond to the app becoming Active?
- Handling back button in Android Navigation Component
- Handling click events on a drawable within an EditText
- Handling Touch Event in UILabel and hooking it up to an IBAction
- Having a UITextField in a UITableViewCell
- Height of status bar in Android
- Hidden Features of Xcode 4
- Hide keyboard when scroll UITableView
.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.