Custom UITableViewCell selection style? Custom UITableViewCell selection style? ios ios

Custom UITableViewCell selection style?


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];    }}


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


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