UITableViewCell subview disappears when cell is selected
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
When a UITableViewCell subview disappears on selection, the cause is usually not that UIKit randomly removes the view. The more common reasons are that the view was added to the wrong part of the cell, the selected background is covering it, or state-handling code in the cell resets layout or visibility during selection.
Add Custom Views to contentView
The safest rule is to add your own subviews to the cell's contentView, not directly to the cell itself.
contentView is the intended container for the cell's visible content.
If a view was added directly to the cell, selection and background transitions can expose that mistake because UIKit manages the cell's visible layers with contentView as the expected content container.
Understand Selection Backgrounds
When a cell becomes selected or highlighted, UIKit can change the background presentation. If your custom subview is behind the selected background view, it can look like it vanished.
You can reduce this problem by:
- Keeping content in
contentView - Using
selectedBackgroundViewcarefully - Setting
selectionStyle = .noneif selection visuals are not needed
Example:
That removes the default selection effect entirely.
Check setSelected and setHighlighted
Custom cell subclasses often override selection-state methods. If those methods hide or reconfigure subviews incorrectly, views can disappear during the selection transition.
This is fine because it changes appearance without removing the view. Problems appear when code resets frames, removes subviews, or rebuilds the hierarchy during state changes.
Reuse Can Make the Bug Look Like Selection
Sometimes the bug is actually a reuse bug. Selecting a cell triggers layout and reuse behavior nearby, and the disappearing subview only looks selection-related.
Make sure every reusable state is reapplied in prepareForReuse or in your cell configuration method:
If the cell is reused after some earlier state change, selection may just be the moment the problem becomes visible.
Check Custom Background Views
If you assign backgroundView or selectedBackgroundView, make sure those views are not masking or replacing the visible content unexpectedly. Custom background configuration is a frequent source of confusion because the cell still "works," but the layering changes when highlight or selection state flips.
If the selection effect is not important to the design, removing it entirely with selectionStyle = .none is often the simplest way to confirm whether the disappearance is caused by selection rendering or by something else such as reuse and layout.
Common Pitfalls
- Adding subviews directly to the cell instead of
contentView. - Letting a selected background view visually cover the content.
- Overriding
setSelectedorsetHighlightedand accidentally undoing view state. - Blaming selection when the real issue is cell reuse or incomplete reconfiguration.
Summary
- Put custom content inside
contentView. - Be aware that selection and highlight states can change cell background presentation.
- Keep
setSelectedandsetHighlightedlightweight and state-safe. - Reset reusable view state explicitly.
- Most disappearing-subview bugs in table cells come from view hierarchy or reuse mistakes, not from selection itself.
Related reading
- UITableViewCell with UITextView height in iOS 7?
- UITableViewHeaderFooterView Unable to change background color
- UITapGestureRecognizer - make it work on touch down, not touch up?
- UITapGestureRecognizer - single tap and double tap
- UITapGestureRecognizer breaks UITableView didSelectRowAtIndexPath
- UITapGestureRecognizer tap on self.view but ignore subviews
- UITextField - capture return button event
- UITextField auto-capitalization type - iPhone App
.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.