3

Following the advice in this answer Custom UITableViewCell selection style? about how to make a custom selection style for a UITableViewCell I did the following.

First, in my custom view controller, which has a UITableView as a property and which serves as its datasource and delegate I have the following:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    //UITableViewCell *cell;
    NavTableViewCell *cell;
    cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

Then this is my entire custom UITableViewCell:

#import "NavTableViewCell.h"

@implementation NavTableViewCell

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
    [super setHighlighted:highlighted animated:animated];
    if(highlighted){
        self.contentView.alpha = .5;
    }else{
        self.contentView.alpha = 1;

    }
}

@end

I have also tried putting the code in setSelected. In both cases I don't see any change in my selected/highlighted table view cell. I have tried changing its color and changing the colors of the labels within it, but there is simply no change. I have also tried going through all the kinds of selection styles for the cell, all to no avail as far as seeing my customization.

What am I doing wrong?

Community
  • 1
  • 1
helloB
  • 3,472
  • 10
  • 40
  • 87

2 Answers2

7

From the documentation:

You can use selectedBackgroundView to give the cell a custom appearance when it is selected. When the cell is selected, this view is layered above the backgroundView and behind the contentView.

So whatever you want to do to your cell while selecting it, you have to do it for the selectedBackgroundView .

Teja Nandamuri
  • 11,045
  • 6
  • 57
  • 109
0

Try this:

UIView *highlightView = [[UIView alloc] init];
highlightView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:1.0 alpha:.5];
cell.selectedBackgroundView = highlightView;
Abhinav
  • 37,684
  • 43
  • 191
  • 309
  • thanks for the suggestion. That does something but it doesn't achieve the effect I want, which is for the entire contentView and enclosed subviews to change their alpha value. – helloB Oct 01 '15 at 15:44