开发者

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 cl开发者_运维技巧earcolor];

but it says that this method is deprecated.


In Swift:

cell.selectionStyle = UITableViewCell.SelectionStyle.none

or simply:

cell.selectionStyle = .none


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


Remove the cell highlight color of UITableView


Swift 3.0

cell.selectionStyle = .none


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

Remove the cell highlight color of UITableView


// Swift 2.0

cell.selectionStyle = UITableViewCellSelectionStyle.None


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


Do it in the cell:

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

    ...
}

It's that easy.


Try this for swift

cell?.selectionStyle = UITableViewCellSelectionStyle.None


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
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜