By default UITableViewCells in a UITableViewCell are selectable. In other words, the cell gets colored when the user clicks on it. If you want to disable that you can do this in 2 ways:
- Disable selection for all UITableViewCells in the UITableView
- Disable selection for specific UITableViewCells.
Disable selection for all UITableViewCells in the UITableView
To disable selection for all UITableViewCells in your UITableView. You have to set the following code in for example the ViewDidLoad function of your ViewController:
tableView.allowsSelection = false
This will look like this:
override func viewDidLoad() { super.viewDidLoad() ... tableView.allowsSelection = false }
Disable selection for specific UITableViewCells
If you only want to disable the selection of some of the UITableViewCells in your UITableView you can define this for each cell in the tableView function with the cellForRowAt parameter. With the following code you can disable selection for a single cell:
cell.selectionStyle = UITableViewCell.SelectionStyle.none
When implemented, this will look like this:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { guard let cell = tableView.dequeueReusableCell(withIdentifier: "MyTableViewCellIdentifier", for: indexPath) as? MyTableViewCell else { fatalError("The dequeued cell is not an instance of " + MyTableViewCell.description()) } ... cell.selectionStyle = UITableViewCell.SelectionStyle.none ... return cell }