ChatGPT解决这个技术问题 Extra ChatGPT

Remove the cell highlight color of UITableView

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.

If you google here, be aware that the extremely old (10 years plus) selected answer here, is simply wrong.

p
pkamb

In Swift:

cell.selectionStyle = UITableViewCell.SelectionStyle.none

or simply:

cell.selectionStyle = .none

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.
p
pkamb

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

https://i.stack.imgur.com/u41qR.png


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
R
Rashwan L

Swift 3.0

cell.selectionStyle = .none

U
Unihedron
// Swift 2.0

cell.selectionStyle = UITableViewCellSelectionStyle.None

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

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

https://i.stack.imgur.com/M4L9R.png


Swift 4 : cell.selectionStyle = UITableViewCell.SelectionStyle.none
j
joe

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


F
Fattie

Do it in the cell:

class YourCell:  UITableViewCell {
    
    override func didMoveToSuperview() {
        selectionStyle = .none
    }

    ...
}

It's that easy.


J
Jugal K Balara

Try this for swift

cell?.selectionStyle = UITableViewCellSelectionStyle.None

f
fingia

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.


z
zeytin

Swift 5.4 , just put the selectionStyle = .none

Example:

class TableViewCell: UITableViewCell {

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

}

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

Swift 5:

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