Custom UITableViewCell selection style?

IosObjective CUitableviewClickSelection

Ios Problem Overview


When I click on my UITableViewCell, the background part (the areas that my background image doesn't cover) turns blue when I click on the cell. Also, all of the UILabels on the cell turn to white when it is clicked which is what I want.

However what I do not want is the blue background when I click it but if I do selectionstylenone, then I lose the highlighted colours for the UILabels in the cell.

So is there any way to just get rid of the blue background when the cell is clicked but to keep the highlighted colors of the UILabels?

Ios Solutions


Solution 1 - Ios

You can do this as follows. Set your table cell's selection style to UITableViewCellSelectionStyleNone. This will remove the blue background highlighting. Then, to make the text label highlighting work the way you want, instead of using the default UITableViewCell class, create a subclass of UITableViewCell and override the default implementation of setHighlighted:animated with your own implementation that sets the label colors to however you want depending on the highlighted state.

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

Solution 2 - Ios

If working before iOS7, make your cell selection style none

cell.selectionStyle = UITableViewCellSelectionStyleNone;

Else, leave it by UITableViewCellSelectionStyleDefault

Then:

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

This code will work properly

Solution 3 - Ios

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

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

Implement your code here, like this

-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    CustomCell *cell = (CustomCell *)[tableView cellForRowAtIndexPath:indexPath];
    [cell.lbls setTextColor:[UIColor whiteColor]];
    return indexPath;
}

Solution 4 - Ios

To get this work you have to set the selection style to UITableViewCellSelectionStyleNone and then you should override the method setSelected:animated: to get the result you want. It does the same thing the automated selection mechanism of iOS does when you see the 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 customize this in another way, e.g. by changing the UITableViewCell background, etc.

Solution 5 - Ios

In cellForRowAtIndexPath use this code:

[cell setSelectionStyle:UITableViewCellSelectionStyleNone];

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

Hope this will work for you.

Enjoy Coding :)

Solution 6 - Ios

Overriding the following functions in your subclass of UITableViewCell.

override func setHighlighted(highlighted: Bool, animated: Bool) { }
override func setSelected(selected: Bool, animated: Bool) { }

Solution 7 - Ios

In order to match the standard selection style behavior, you'll want to override both setHighlighted:animated: and setSelected:animated:. You'll probably want to move that code into a shared method in order to avoid duplicate code.

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()
    }
}

Solution 8 - Ios

In your custom cell, override the default implementation of awakeFromNib & 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.

Solution 9 - Ios

The only way I could get it working 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;
}

Solution 10 - Ios

Also you can use contentView.alpha. Here is the example.

First, set selection style for your cell:

[cell setSelectionStyle:UITableViewCellSelectionStyleNone];

Next, in custom cell class override this method with animation example:

  - (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
    [super setHighlighted:highlighted animated:animated];

    if (highlighted) {
        [UIView animateWithDuration:0.15f animations:^{
            self.contentView.alpha = 0.5f;
        }];
    } else {
        [UIView animateWithDuration:0.35f animations:^{
            self.contentView.alpha = 1.f;
        }];
    }
  }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSimplyKiwiView Question on Stackoverflow
Solution 1 - IosjonkrollView Answer on Stackoverflow
Solution 2 - IosAlfaView Answer on Stackoverflow
Solution 3 - IosScarView Answer on Stackoverflow
Solution 4 - Ioswho9vyView Answer on Stackoverflow
Solution 5 - IosMrunalView Answer on Stackoverflow
Solution 6 - Iossuperarts.orgView Answer on Stackoverflow
Solution 7 - IosLogan GauthierView Answer on Stackoverflow
Solution 8 - IoszeeawanView Answer on Stackoverflow
Solution 9 - IosRudolf RealView Answer on Stackoverflow
Solution 10 - IosAlex KolovatovView Answer on Stackoverflow