UITableViewCell custom selection style?

When I click on a UITableViewCell , the background part (areas that the background image does not cover) turns blue when I click on the cell. In addition, all UILabel on the cell turn white when they click what I want.

However, I do not need a blue background when I click on it, but if I do a selectionstylenone , I lose the highlighted colors for the UILabel in the cell.

So, is there a way to just get rid of the blue background when a cell is clicked, but keep the highlighted colors of UILabel s?

+61
ios objective-c uitableview click selection
Aug 12 '12 at 6:23
source share
9 answers

You can do it as follows. Set the cell selection style of the UITableViewCellSelectionStyleNone table cell. This will remove the blue background. Then, to make the text label selection the way you want, instead of using the default UITableViewCell class, create a subclass of UITableViewCell and redefine the standard implementation of setHighlighted:animated with your own implementation, which sets the colors of the labels, however you want depending on the selected state.

 - (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated { if (highlighted) { self.textLabel.textColor = [UIColor whiteColor]; } else { self.textLabel.textColor = [UIColor blackColor]; } } 
+147
Aug 12 2018-12-12T00:
source share

If you work before iOS7, do not do cell selection style

 cell.selectionStyle = UITableViewCellSelectionStyleNone; 

Left, leave it UITableViewCellSelectionStyleDefault

Then:

 UIView *selectedView = [[UIView alloc]init]; selectedView.backgroundColor = [UIColor redColor]; cell.selectedBackgroundView = selectedView; 

This code will work correctly

+65
Aug 14 '13 at 7:14
source share

You can use the following delegate methods after you set the select style to none:

 -(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath 

Enter your code here, for example

 -(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath { CustomCell *cell = (CustomCell *)[tableView cellForRowAtIndexPath:indexPath]; [cell.lbls setTextColor:[UIColor whiteColor]]; return indexPath; } 
+15
Aug 12 2018-12-12T00:
source share

In cellForRowAtIndexPath use this code:

 [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; [cell.myLabel setHighlightedTextColor: [UIColor whiteColor]]; // for all your labels 

Hope this works for you.

Enjoy the coding :)

+10
Aug 12 '12 at 7:21
source share

To get this working, you must set the selection style to UITableViewCellSelectionStyleNone , and then you must override the setSelected:animated: method to get the desired result. This does the same thing as the iOS auto-select mechanism when you see a blue (or gray) selection.

 - (void)setSelected:(BOOL)selected animated:(BOOL)animated { if (selected) { self.textLabel.textColor = [UIColor whiteColor]; } else { self.textLabel.textColor = [UIColor blackColor]; } } 

You can also configure it differently, for example. by changing the background of a UITableViewCell, etc.

+8
Jan 10 '13 at 21:25
source share

Overriding the following functions in your subclass of UITableViewCell .

 override func setHighlighted(highlighted: Bool, animated: Bool) { } override func setSelected(selected: Bool, animated: Bool) { } 
+2
May 09 '15 at 6:46
source share

To match the standard behavior of the selection style, you need to override both setHighlighted:animated: and setSelected:animated: You will probably want to move this code to a generic method to avoid code duplication.

 override func setHighlighted(highlighted: Bool, animated: Bool) { setAsSelectedOrHighlighted(highlighted, animated: animated) super.setHighlighted(highlighted, animated: animated) } override func setSelected(selected: Bool, animated: Bool) { setAsSelectedOrHighlighted(selected, animated: animated) super.setSelected(selected, animated: animated) } func setAsSelectedOrHighlighted(selectedOrHighlighted: Bool, animated: Bool) { let action = { // Set animatable properties } if animated { UIView.animateWithDuration(1.0, delay: 0, options: .CurveEaseInOut, animations: action, completion: nil) } else { action() } } 
+1
Feb 09 '16 at 19:46
source share

In your custom cell, override the standard implementation of awakeFromNib and setSelected:

 - (void)awakeFromNib { // Initialization code UIImageView * imageView = [[UIImageView alloc] initWithFrame:self.bounds]; imageView.image = [UIImage imageNamed:@"cell_selected_img"]; self.selectedBackgroundView = imageView; } - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; // Configure the view for the selected state if (selected) { self.lblCustomText.textColor = [UIColor whiteColor]; } else { self.lblCustomText.textColor = [UIColor blackColor]; } } 

Also make sure that the selection style is NOT set to None .

0
Jan 23 '15 at 3:28
source share

The only way I could work was by:

 - (void)awakeFromNib { UIView *bgColorView = [[UIView alloc] init]; bgColorView.backgroundColor = [UIColor colorWithRed:(55.0/255.0) green:(163.0/255.0) blue:(237.0/255.0) alpha:1.0]; bgColorView.layer.masksToBounds = YES; self.selectedBackgroundView = bgColorView; } 
0
Apr 27 '15 at 17:28
source share



All Articles