UITableViewCell
iOS development
subview issue
cell selection
Swift programming

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.

Browse interview questions

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.

swift
1final class CustomCell: UITableViewCell {
2    private let badge = UILabel()
3
4    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
5        super.init(style: style, reuseIdentifier: reuseIdentifier)
6
7        badge.text = "New"
8        badge.translatesAutoresizingMaskIntoConstraints = false
9        contentView.addSubview(badge)
10
11        NSLayoutConstraint.activate([
12            badge.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -16),
13            badge.centerYAnchor.constraint(equalTo: contentView.centerYAnchor)
14        ])
15    }
16
17    required init?(coder: NSCoder) {
18        fatalError("init(coder:) has not been implemented")
19    }
20}

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 selectedBackgroundView carefully
  • Setting selectionStyle = .none if selection visuals are not needed

Example:

swift
cell.selectionStyle = .none

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.

swift
1override func setSelected(_ selected: Bool, animated: Bool) {
2    super.setSelected(selected, animated: animated)
3    badge.alpha = selected ? 0.7 : 1.0
4}

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:

swift
1override func prepareForReuse() {
2    super.prepareForReuse()
3    badge.isHidden = false
4    badge.alpha = 1.0
5}

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 setSelected or setHighlighted and 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 setSelected and setHighlighted lightweight 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
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.