How can I disable the UITableView selection?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Disabling selection in a UITableView can mean a few different things. Sometimes you want rows to be completely non-selectable, sometimes you want to keep tap handling but remove the gray highlight, and sometimes only specific rows should ignore taps. UIKit supports all three cases, but they use different APIs.
Disable selection for the whole table
If no row in the table should ever become selected, set allowsSelection to false.
This is the cleanest option for read-only tables. If allowsSelection is false, didSelectRowAt will not be called for normal row taps.
There is a related property called allowsSelectionDuringEditing. Use that separately if the table enters editing mode and your selection rules change there.
Remove the highlight but keep the row interactive
Sometimes the row still performs an action, but you do not want the default selected appearance. In that case, do not disable table selection globally. Instead, change the cell's selection style.
This is a very common point of confusion: selectionStyle = .none changes appearance, not behavior. The row can still be selected and your delegate method can still run.
Disable selection only for certain rows
If some rows should respond to taps and others should not, implement willSelectRowAt and return nil for the rows you want to block.
That gives row-level control without changing the rest of the table. It is usually a better fit than toggling allowsSelection on and off as the user scrolls.
You can combine this with selectionStyle = .none so blocked rows also look non-interactive.
Keep the UX consistent
The technical property is easy. The harder part is making the interface communicate the right affordance.
If a row does nothing, it should generally not look tappable. If a row still navigates somewhere or reveals details, removing every visual cue can make the table feel broken. In many cases, the better solution is to keep selection and simply deselect the row after the action:
That preserves the standard iOS interaction pattern while avoiding a row that stays highlighted.
Also remember that cells can contain controls such as switches, buttons, and steppers. Disabling row selection does not automatically disable those controls, which is often exactly what you want in settings-style screens.
Common Pitfalls
The most common mistake is setting selectionStyle = .none and assuming row selection itself is disabled. It is not.
Another mistake is disabling allowsSelection globally when only a few rows should ignore taps. That usually removes useful behavior from the rest of the screen.
People also forget about editing mode. A table that should not allow normal selection may still need allowsSelectionDuringEditing configured explicitly.
Finally, removing the highlight without replacing it with any other visual cue can make interactive rows feel dead even though they still work.
Summary
- Use
tableView.allowsSelection = falseto disable row selection entirely. - Use
cell.selectionStyle = .nonewhen you only want to remove the highlight. - Return
nilfromwillSelectRowAtfor row-specific control. - Call
deselectRowif you want standard tap behavior without a lingering highlight. - Match the visual affordance of each row to what it actually does.
Related reading
- How can I display a list view in an Android Alert Dialog?
- How can I do Key Value Observing and get a KVO callback on a UIView's frame?
- How can I easily delete all objects in a Realm
- How can I enable zoom in on UIWebView which inside the UIScrollView?
- How can I encode/decode a string to Base64 in Swift?
- How can I extend typed Arrays in Swift?
- How can I fix 'android.os.NetworkOnMainThreadException'?
- How can I fix unexpected element queries found in manifest error?
.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.