简体   繁体   中英

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.

cell.selectionStyle = UITableViewCellSelectionStyleNone;

in Swift 4 updated

cell.selectionStyle = UITableViewCell.SelectionStyle.none

Or

cell.selectionStyle = .none

Select this in Storyboard or XIB


在此处输入图片说明

// Swift 2.0

cell.selectionStyle = UITableViewCellSelectionStyle.None

斯威夫特 3.0

cell.selectionStyle = .none

Objective-C:

cell.selectionStyle = UITableViewCellSelectionStyleNone;

or

[cell setSelectionStyle:UITableViewCellSelectionStyleNone];

Swift 4:

self.selectionStyle = UITableViewCellSelectionStyle.none;

Swift 3:

cell.selectionStyle = .none

Swift 2:

cell.selectionStyle = UITableViewCellSelectionStyle.None

If you want to change it just using Storyboard/Xib, just 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

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

试试这个快速

cell?.selectionStyle = UITableViewCellSelectionStyle.None

Do it in the cell :

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

    ...
}

It's that easy.

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.

Swift 5.4 , just put the selectionStyle = .none

Example:

class TableViewCell: UITableViewCell {

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

}

swift 5

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

        return cell
    }  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM