156

I want to remove the default blue color of uitableview cell selection. I don't want any selection color there. I have not created a custom cell class. I'm customizing the cell by adding labels and buttons over it. I tried doing:

cell.selectioncolor = [UIColor clearcolor]; 

but it says that this method is deprecated.

3
  • If you google here, be aware that the extremely old (10 years plus) selected answer here, is simply wrong. Commented Nov 13, 2021 at 14:46
  • If you google here, be aware that the above comment is not true. Commented Jul 13, 2023 at 17:44
  • LOL man :) you know I think there is some mass confusion on this QA between the various types of "selection / clicking / etc" (certainly, it may be me confused) Commented Sep 8, 2023 at 13:10

11 Answers 11

416

In Swift:

cell.selectionStyle = UITableViewCell.SelectionStyle.none 

or simply:

cell.selectionStyle = .none 
Sign up to request clarification or add additional context in comments.

6 Comments

I kept that code in cellForRowAtIndexPath:, is that the exact place to keep?
@Pawriwes, yes. If I remember correctly you can also set that property in cell's XIB if you create your cell using Interface Builder
If I do so, when editing, it do not select the cell :(
This answer is basically wrong. You do it in the cell subclass. You would never do it in the VC once it has been built.
Answer by @Fattie deserves more recognition. If you place that cell.selectionStyle = .none code in VC, you'll see the default color show up when you select the cell. You must put that line in your cell subclass if what you want is your custom color showing up.
|
74

In the Storyboard or XIB Attributes Inspector, set Selection to None.


Selection: None

3 Comments

additional, you have to check "Table View Cell" if you change in "table view" than you don't select cells.
@Pulkit that is not true. This will only disable the selection state of the cell. This has nothing to do with the data being displayed in it or handling it's interaction. Check to make sure you are have all proper registration set and aren't overriding other functions.
ensure you do this for the cell and not the tableview
15

Objective-C:

cell.selectionStyle = UITableViewCellSelectionStyleNone; // or [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; 

Swift 3+:

cell.selectionStyle = UITableViewCellSelectionStyle.none; // or cell.selectionStyle = .none 

Swift 2:

cell.selectionStyle = UITableViewCellSelectionStyle.None 

If you want to change it just using Interface Builder Storyboard/Xib, select the cell that you want to remove the "Selection Style Effect" and define it as "None". It'll work like magic as well :D

Storyboard / Xib

1 Comment

Swift 4 : cell.selectionStyle = UITableViewCell.SelectionStyle.none
12

Swift 3.0

cell.selectionStyle = .none 

Comments

11
// Swift 2.0 cell.selectionStyle = UITableViewCellSelectionStyle.None 

1 Comment

Or you can use the enum value directly cell.selectionStyle = .None
8

Do note that every answer which mentions "selectionStyle" is completely wrong.

selectionStyle determines whether or not you can click on the cell.

It has nothing to do with whether the cell is turned "gray" when you click on it.

The only way to turn off the "gray effect" (when you click on it) is like this:

class YourCell: UITableViewCell { override func didMoveToSuperview() { selectedBackgroundView?.isHidden = true } ... } 

Note that selectedBackgroundView is indeed available on the storyboard.

I wouldn't fool with it because the lifecycle is so strange, but note that these days, indeed you can get at selectedBackgroundView on the storyboard. It's possible you could handle it in there.

The answer by @fingia, see below.

Regarding the question on this page, how to "remove the cell highlight color", notice that the @fingiua answer correctly states that you adjust selectedBackgroundView. However, it states that what you do is change the background color.

This used to work in the past to some extent and is still possible in certain situations but it's a hell of a task figuring out when in the lifecycle you have to set the color - because, UIKit itself is constantly setting the color! (For example, if you do it in the obvious didMoveToSuperview or prepareForReuse ... it simply doesn't work.)

This is a great example of how, on the internet, for obscure yet critical issues, there is often a lot of VERY OLD example code on the internet, which gets copied endlessly, which is either just wrong or "half wrong", but still gets copied endlessly.

There's no reason at all you'd fool with the color, because what you want to do is simply ... hide it. So just hide it. This (of course) works reliably (indeed, you can do so anywhere at all in the view cycle, once it's hidden that's the end of it).

9 Comments

Check documentation: developer.apple.com/documentation/uikit/uitableviewcell/… - it specifically says that property controls only cell presentation (i.e. background view) on selection and does not affect how cell responds to the selection
Sorry @Fattie but your assertion is the one that is incorrect here. You can set selectionStyle to .none and still select the row. The currently highest voted and selected answer is not technically wrong. Please read the documentation for UITableViewCell selectionStyle and UITableViewCell.SelectionStyle. Your updated answer here is no better than setting selectionStyle to .none. It only hides the selection, it doesn't prevent the selection.
While I don't understand why 10 answers are needed to state that you can use selectionStyle, if someone actually wants to prevent cell selection, the correct solution is to return nil from the willSelectRowAt delegate method.
hi @HangarRash . Regarding your first comment. You may have totally misread the question. Here's the question: Remove the cell highlight color the OP is not saying anything about whether or not you can select a row in general (which is what the selectionStyle property affects) nor does it say anything about whether you can select specific rows (you achieve that with the technique mentioned in your second comment).
hi @Vladimir - please read the question title
|
7

Setting the TableView Selection style to .none was affecting the responsiveness and performance of the tableview in my app (didSelectRowAt indexPath taps were getting delayed). My solution to this problem was to hide the selected background view on awakeFromNib() when the cell is first created:

selectedBackgroundView?.isHidden = true

Comments

4

Try this for swift

cell?.selectionStyle = UITableViewCellSelectionStyle.None 

Comments

3

right answer should be:

cell.selectedBackgroundView?.backgroundColor = <choose your color>

The selection type is a different property that when set to .none produces what you want PLUS other unwanted side-effects.

If you do not want the cell to be highlighted, then make its background view's color the same as when it is not highlighted.

1 Comment

fingia, this is no longer exactly correct. you simply set it once to "isHidden" and you're done.
3

Swift 5.4 , just put the selectionStyle = .none

Example:

class TableViewCell: UITableViewCell { override func awakeFromNib() { super.awakeFromNib() selectionStyle = .none } 

2 Comments

working for me.......
This is wrong. gist.github.com/natecook1000/ca64e1dc4fa41adfc603 . The correct approach if you're changing a style is the one in my answer above.
2

Swift 5:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "cell") cell.selectionStyle = .none return cell } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.