Remove the cell highlight color of UITableView
IosObjective CUitableviewIos Problem Overview
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.
Ios Solutions
Solution 1 - Ios
In Swift:
cell.selectionStyle = UITableViewCell.SelectionStyle.none
or simply:
cell.selectionStyle = .none
Solution 2 - Ios
Solution 3 - Ios
Swift 3.0
cell.selectionStyle = .none
Solution 4 - Ios
// Swift 2.0
cell.selectionStyle = UITableViewCellSelectionStyle.None
Solution 5 - Ios
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
Solution 6 - Ios
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
Solution 7 - Ios
Do it in the cell:
class YourCell: UITableViewCell {
override func didMoveToSuperview() {
selectionStyle = .none
}
...
}
It's that easy.
Solution 8 - Ios
Try this for swift
cell?.selectionStyle = UITableViewCellSelectionStyle.None
Solution 9 - Ios
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.
Solution 10 - Ios
Swift 5.4 , just put the selectionStyle = .none
Example:
class TableViewCell: UITableViewCell {
override func awakeFromNib() {
super.awakeFromNib()
selectionStyle = .none
}
Solution 11 - Ios
Swift 5:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell")
cell.selectionStyle = .none
return cell
}