Change the background color of the selected cell?

Does anyone know how to change the background color of a cell using a UITableViewCell for each selected cell? I created this UITableViewCell inside the TableView code.

+48
colors iphone uitableview background tableview
Mar 10 '10 at 15:41
source share
26 answers

Changing the selectedBackgroundView property is the correct and easiest way. I use the following code to change the highlight color:

// set selection color UIView *myBackView = [[UIView alloc] initWithFrame:cell.frame]; myBackView.backgroundColor = [UIColor colorWithRed:1 green:1 blue:0.75 alpha:1]; cell.selectedBackgroundView = myBackView; [myBackView release]; 
+92
Jul 05 '10 at 4:45
source share

I finally managed to get this to work in a table view with the style set in Grouped.

First set the selectionStyle property to the property of all UITableViewCellSelectionStyleNone cells.

 cell.selectionStyle = UITableViewCellSelectionStyleNone; 

Then, do the following in the table view delegate:

 static NSColor *SelectedCellBGColor = ...; static NSColor *NotSelectedCellBGColor = ...; - (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSIndexPath *currentSelectedIndexPath = [tableView indexPathForSelectedRow]; if (currentSelectedIndexPath != nil) { [[tableView cellForRowAtIndexPath:currentSelectedIndexPath] setBackgroundColor:NotSelectedCellBGColor]; } return indexPath; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [[tableView cellForRowAtIndexPath:indexPath] setBackgroundColor:SelectedCellBGColor]; } - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { if (cell.isSelected == YES) { [cell setBackgroundColor:SelectedCellBGColor]; } else { [cell setBackgroundColor:NotSelectedCellBGColor]; } } 
+37
Feb 17 '12 at 8:25
source share
 // animate between regular and selected state - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; if (selected) { self.backgroundColor = [UIColor colorWithRed:234.0f/255 green:202.0f/255 blue:255.0f/255 alpha:1.0f]; } else { self.backgroundColor = [UIColor clearColor]; } } 
+19
Jul 04 '13 at 12:25
source share

SWIFT 4, XCODE 9, iOS 11

After some testing, this WILL remove the background color if you deselect or touch the cell a second time when the Selection table view is set to "Multiple Selection". Also works when the table presentation style is set to "Grouped".

 extension ViewController: UITableViewDelegate { func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { if let cell = tableView.cellForRow(at: indexPath) { cell.contentView.backgroundColor = UIColor.darkGray } } } 

Note. In order for this to work, as shown below, any BUT value can be set for the Cell Select property.

How it looks with different options

Style: Regular , Selection: Single Selection

Single selection

Style: Normal , Selection: Multiple Choice

Multiple selection

Style: grouped , selection: multiple choice

Grouped Multiple Selection

Bonus - Animation

For a smoother color transition, try the animation:

 extension ViewController: UITableViewDelegate { func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { if let cell = tableView.cellForRow(at: indexPath) { UIView.animate(withDuration: 0.3, animations: { cell.contentView.backgroundColor = UIColor.darkGray }) } } } 

Animated color transition

Bonus - change text and image

You may notice that the icon and text color also change when you select a cell. This happens automatically when you set the UIImage and UILabel Highlighted properties

UIImage

  1. Put two color images:

Two colored images

  1. Set the Highlighted Image property:

Highlighted property

Uilabel

Just specify a color for the selected object:

Highlighted color

+12
Feb 28 '18 at 5:01
source share
 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = (UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath]; cell.contentView.backgroundColor = [UIColor yellowColor]; } 
+11
Jun 20 '13 at 8:50
source share

I created a UIView and set the selectedBackgroundView cell property:

 UIView *v = [[UIView alloc] init]; v.backgroundColor = [UIColor redColor]; cell.selectedBackgroundView = v; 
+10
Mar 12 '14 at 6:17
source share

If you are talking about individual cells, the -selectedBackgroundView property. This will be displayed when the user selects your cell.

+7
Mar 10 '10 at 16:02
source share

I was lucky with the following:

 - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { bool isSelected = // enter your own code here if (isSelected) { [cell setBackgroundColor:[UIColor colorWithRed:1 green:1 blue:0.75 alpha:1]]; [cell setAccessibilityTraits:UIAccessibilityTraitSelected]; } else { [cell setBackgroundColor:[UIColor clearColor]]; [cell setAccessibilityTraits:0]; } } 
+6
Mar 10 '10 at 16:27
source share

I have a very customized UITableViewCell. Therefore, I implemented my own selection of cells.

 cell.selectionStyle = UITableViewCellSelectionStyleNone; 

I created a method in my cell class:

 - (void)highlightCell:(BOOL)highlight { if (highlight) { self.contentView.backgroundColor = RGB(0x355881); _bodyLabel.textColor = RGB(0xffffff); _fromLabel.textColor = RGB(0xffffff); _subjectLabel.textColor = RGB(0xffffff); _dateLabel.textColor = RGB(0xffffff); } else { self.contentView.backgroundColor = RGB(0xf7f7f7);; _bodyLabel.textColor = RGB(0xaaaaaa); _fromLabel.textColor = [UIColor blackColor]; _subjectLabel.textColor = [UIColor blackColor]; _dateLabel.textColor = RGB(0x496487); } } 

In my UITableViewController class in ViewWillAppear, the following has been added:

 NSIndexPath *tableSelection = [self.tableView indexPathForSelectedRow]; SideSwipeTableViewCell *cell = (SideSwipeTableViewCell*)[self.tableView cellForRowAtIndexPath:tableSelection]; [cell highlightCell:NO]; 

In didSelectRow added:

 SideSwipeTableViewCell *cell = (SideSwipeTableViewCell*)[self.tableView cellForRowAtIndexPath:indexPath]; [cell highlightCell:YES]; 
+6
Jan 14 '13 at 13:55
source share

For iOS7 + and if you use Interface Builder, then subclass your cell and do:

Objective-c

 - (void)awakeFromNib { [super awakeFromNib]; // Default Select background UIView *v = [[UIView alloc] init]; v.backgroundColor = [UIColor redColor]; self.selectedBackgroundView = v; } 

Swift 2.2

 override func awakeFromNib() { super.awakeFromNib() // Default Select background self.selectedBackgroundView = { view in view.backgroundColor = .redColor() return view }(UIView()) } 
+6
Dec 10 '15 at 19:09
source share

This worked great with group calls: Implementing a custom subclass of UITableViewCell

It will respect angles and such ...

 - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; if(selected) [self setBackgroundColor:[UIColor colorWithRed:(245/255.0) green:(255/255.0) blue:(255/255.0) alpha:1]]; else [self setBackgroundColor:[UIColor whiteColor]]; } 
+5
Jul 17 '13 at 20:20
source share

If you just want to remove the background gray, do the following:

 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [[tableView cellForRowAtIndexPath:indexPath] setSelectionStyle:UITableViewCellSelectionStyleNone]; } 
+5
Oct 27 '13 at 12:18
source share

I managed to solve this problem by creating a subclass of UITableViewCell and implementing the setSelected: animated method:

 - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; // Configure the view for the selected state if(selected) { [self setSelectionStyle:UITableViewCellSelectionStyleNone]; [self setBackgroundColor:[UIColor greenColor]]; } else { [self setBackgroundColor:[UIColor whiteColor]]; } } 

Trick set

 cell.selectionStyle = UITableViewCellSelectionStyleDefault; 

in the controller of the implementing view, and then in the ViewCell table set it as

 [self setSelectionStyle:UITableViewCellSelectionStyleNone]; 

Hope this helps. :)

+5
Jul 14 '14 at 16:06
source share

The default style is gray and it destroys the colors of the cell if it was done programmatically. You can do this to avoid this. (in Swift)
cell.selectionStyle = .None

+4
Sep 08 '15 at 4:16
source share

Drop AdvancedTableViewCells in Apple Code Sample .

You want to use a compound cell template.

+3
Apr 20 2018-11-11T00:
source share

In swift

 let v = UIView() v.backgroundColor = self.darkerColor(color) cell?.selectedBackgroundView = v; ... func darkerColor( color: UIColor) -> UIColor { var h = CGFloat(0) var s = CGFloat(0) var b = CGFloat(0) var a = CGFloat(0) let hueObtained = color.getHue(&h, saturation: &s, brightness: &b, alpha: &a) if hueObtained { return UIColor(hue: h, saturation: s, brightness: b * 0.75, alpha: a) } return color } 
+3
Sep 15 '14 at 18:26
source share

in Swift 3, converted from response highlight.

 override func setSelected(_ selected: Bool, animated: Bool) { super.setSelected(selected, animated: animated) if(selected) { self.selectionStyle = .none self.backgroundColor = UIColor.green } else { self.backgroundColor = UIColor.blue } } 

(however, the view does not change until the selection is confirmed, releasing the finger)

+3
Oct 05 '16 at 9:28
source share

Create a custom UITableViewCell. Within the user class, override the setSelected function and change the background color of the contentView . You can also override the "setHighlighted" function.

In Swift:

 class myTableViewCell: UITableViewCell { override func awakeFromNib() { super.awakeFromNib() // Initialization code } override func setSelected(selected: Bool, animated: Bool) { super.setSelected(selected, animated: animated) // Configure the view for the selected state // Add your color here self.contentView.backgroundColor = UIColor.whiteColor() } override func setHighlighted(highlighted: Bool, animated: Bool) { // Add your color here self.contentView.backgroundColor = UIColor.whiteColor() } } 
+2
Jan 14 '16 at 21:26
source share

Works for me

 UIView *customColorView = [[UIView alloc] init]; customColorView.backgroundColor = [UIColor colorWithRed:180/255.0 green:138/255.0 blue:171/255.0 alpha:0.5]; cell.selectedBackgroundView = customColorView; 
+2
May 17 '16 at 7:23
source share

Here's a quick way to do it right in Interface Builder (inside the storyboard). Drag a simple UIView to the top of your UITableView, as in Uiview Then connect your selectedBackgroundView Output cell to this view. You can even connect the outputs of several cells to this view. Cell's outlet

+1
Sep 24 '14 at 20:04
source share

For a solution that works (correctly) with UIAppearance for iOS 7 (and higher?) By subclassing UITableViewCell and using your default selectedBackgroundView to set the color, look at my answer to a similar question here .

+1
Aug 26 '15 at 10:15
source share
 - (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = (UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath]; cell.contentView.backgroundColor = [UIColor yellowColor]; } - (void)tableView:(UITableView *)tableView didUnhighlightRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = (UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath]; cell.contentView.backgroundColor = nil; } 
+1
Apr 05 '16 at 5:53 on
source share
 override func setSelected(_ selected: Bool, animated: Bool) { super.setSelected(selected, animated: animated) if selected { self.contentView.backgroundColor = .black } else { self.contentView.backgroundColor = .white } } 
+1
Nov 15 '17 at 21:09 on
source share

I tried each of the answers above, but none of them are suitable for me,

then I looked at one of the built-in methods and it works fine.

first make cellSelectionStyle equal to None, and then move on to this solution.

 func tableView(_ tableView: UITableView, willDeselectRowAt indexPath: IndexPath) -> IndexPath? { let cell = tableView.cellForRow(at: indexPath); //cell which is getting deselected, make whatever changes that are required to make it back normal cell.backgroundColor = kNormalColor; return indexPath; } func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? { let cell = tableView.cellForRow(at: indexPath); //cell which is getting selected, make whatever changes that are required to make it selected cell.backgroundColor = kSelectedColor; return indexPath; } 

The advantage of these methods over others:

  • It works to select multiple cells
  • You can change any element, whatever you want, not only the background color of this cell, when it is selected, but also canceled.
0
Nov 22 '16 at 12:57
source share

Swift 3, 4, 5 select cell background color

1) Change only the highlighted color when the user clicks on the cell:

1.1) Inside the cell class:

 override func awakeFromNib() { super.awakeFromNib() // Initialization code let backgroundView = UIView() backgroundView.backgroundColor = UIColor.init(white: 1.0, alpha: 0.1) selectedBackgroundView = backgroundView } 

1.2) The viewcontroller that you are using the configured cell

 override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { tableView.deselectRow(at: indexPath, animated: true) } 

2) If you want to set the color for the selected cells:

 override func setSelected(_ selected: Bool, animated: Bool) { super.setSelected(selected, animated: animated) // Configure the view for the selected state if selected { self.backgroundColor = .darkGray } else { self.backgroundColor = .white } } 
0
May 16 '19 at 10:22
source share
 var last_selected:IndexPath! 

define last_selected: IndexPath inside the class

 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { let cell = tableView.cellForRow(at: indexPath) as! Cell cell.contentView.backgroundColor = UIColor.lightGray cell.txt.textColor = UIColor.red if(last_selected != nil){ //deselect let deselect_cell = tableView.cellForRow(at: last_selected) as! Cell deselect_cell.contentView.backgroundColor = UIColor.white deselect_cell.txt.textColor = UIColor.black } last_selected = indexPath } 
0
Aug 20 '19 at 12:37
source share



All Articles