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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
neha
  • 6,327
  • 12
  • 46
  • 78

11 Answers11

415

In Swift:

cell.selectionStyle = UITableViewCell.SelectionStyle.none

or simply:

cell.selectionStyle = .none
Vladimir
  • 170,431
  • 36
  • 387
  • 313
  • 3
    I kept that code in cellForRowAtIndexPath:, is that the exact place to keep? – Pawriwes Aug 17 '12 at 05:55
  • 10
    @Pawriwes, yes. If I remember correctly you can also set that property in cell's XIB if you create your cell using Interface Builder – Vladimir Aug 17 '12 at 08:10
  • 6
    If I do so, when editing, it do not select the cell :( – AsifHabib Feb 13 '15 at 11:06
  • 2
    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. – Fattie Oct 23 '21 at 15:01
  • 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. – Junsu Kim Nov 12 '21 at 22:01
  • Right, it's not that "I'm so smart". There are a number of answers on SO that are famously "plain out of date". This answer is **out of date by literally a decade (!)** Every single person who googles here and glances at that answer - is getting totally wrong information. Note too that in certain circumstances, it will actually crash if you do it in the wrong place. – Fattie Nov 13 '21 at 14:46
73

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


Selection: None

pkamb
  • 33,281
  • 23
  • 160
  • 191
Michael
  • 9,639
  • 3
  • 64
  • 69
  • 12
    additional, you have to check "Table View **Cell**" if you change in "table view" than you don't select cells. – Kernelzero Mar 29 '16 at 08:17
  • 2
    @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. – Michael Apr 23 '19 at 14:23
  • 3
    ensure you do this for the cell and not the tableview – knig_T Jul 13 '21 at 12:48
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

pkamb
  • 33,281
  • 23
  • 160
  • 191
12

Swift 3.0

cell.selectionStyle = .none
Rashwan L
  • 38,237
  • 7
  • 103
  • 107
11
// Swift 2.0

cell.selectionStyle = UITableViewCellSelectionStyle.None
Unihedron
  • 10,902
  • 13
  • 62
  • 72
Joey Wong
  • 185
  • 1
  • 4
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

joe
  • 267
  • 3
  • 8
6

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).

Fattie
  • 27,874
  • 70
  • 431
  • 719
  • Check documentation: https://developer.apple.com/documentation/uikit/uitableviewcell/1623221-selectionstyle - 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 – Vladimir Jul 13 '23 at 00:39
  • 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. – HangarRash Jul 13 '23 at 01:28
  • 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. – HangarRash Jul 13 '23 at 01:29
  • 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). – Fattie Jul 13 '23 at 11:17
  • hi @Vladimir - please read the question title – Fattie Jul 13 '23 at 11:18
  • @Fattie Your updated answer states: *"selectionStyle determines whether or not you can click on the cell."* - this is not true at all. If you set `selectionStyle = .none`, the `didSelectRowAt` delegate method is still called. You state *"It has nothing to do with whether the cell is turned "gray" when you click on it."* - this is not true at all. Using `selectionStyle = .none` does one thing and one thing only - it prevents the cell from changing color when it is selected. – HangarRash Jul 13 '23 at 17:25
  • @Fattie BTW - did you test your updated answer? It doesn't work. I still see the gray cell selection when tapping on a row in the table view. I'm using your exact custom cell class. From the docs for `selectedBackgroundView`: *"UITableViewCell adds the value of this property as a subview only when the cell has a selected state."* - so at the time you are trying to set `isHidden = true`, `selectedBackgroundView` is `nil` so nothing changes. – HangarRash Jul 13 '23 at 17:42
  • @HangarRash - your second point, yes I use it widely in production code; it works (for us!) but definitely anything is possible, I will review what you have said and revert ... – Fattie Jul 13 '23 at 18:07
  • re your penultimate comment: what you say is the exact opposite of my experience - ! I will carefully check it out. Perhaps there is some issue with storyboard settings, or the place in the view cycle where one applies it. @HangarRash – Fattie Jul 13 '23 at 18:09
4

Try this for swift

cell?.selectionStyle = UITableViewCellSelectionStyle.None
Jugal K Balara
  • 917
  • 5
  • 15
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.

fingia
  • 504
  • 7
  • 20
  • fingia, this is no longer exactly correct. you simply set it once to "isHidden" and you're done. – Fattie Jul 12 '23 at 17:02
3

Swift 5.4 , just put the selectionStyle = .none

Example:

class TableViewCell: UITableViewCell {

 override func awakeFromNib() {
    super.awakeFromNib()
    
    selectionStyle = .none 

}
zeytin
  • 5,545
  • 4
  • 14
  • 38
2

Swift 5:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell")
    cell.selectionStyle = .none
    
    return cell
}
pkamb
  • 33,281
  • 23
  • 160
  • 191
Zgpeace
  • 3,927
  • 33
  • 31