1

I have a subclass of UITableViewCell that gives me custom styling and extra data.

All works well when the user interacts, however when I programmatically select it by calling selectRowAtIndexPath it reverts back to the standard selected UITableViewCell with blue background.

Here is my code

[myTable selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];
Rahul Patel
  • 5,858
  • 6
  • 46
  • 72
PaulMrG
  • 1,822
  • 2
  • 16
  • 20

5 Answers5

1

See this question: UITableView Cell selected Color

As a workaround, you can override didSelectRowAtIndexPath and set background color there:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.contentView.backgroundColor = [UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:1.0];

To set backgroungColor to normal color override didDeselectRowAtIndexPath:

-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath  
Community
  • 1
  • 1
Andrey Gordeev
  • 30,606
  • 13
  • 135
  • 162
0

If you have implemented didSelectRowAtIndexPath, then after calling :

[myTable selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];

add this line:

[[myTable delegate]tableView:myTable selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];

write:

cell.selectionStyle = UITableViewCellSelectionStyleNone;
NightFury
  • 13,436
  • 6
  • 71
  • 120
  • I have done that already but it has no effect on the selected style – PaulMrG Sep 09 '13 at 05:24
  • That's removed all styling – PaulMrG Sep 09 '13 at 05:33
  • if you have done some custom styling of yourself, it should override default – NightFury Sep 09 '13 at 05:34
  • @PaulMrG just use `cell.selectionStyle = UITableViewCellSelectionStyleNone;`. Don't set selectedBackground to nil. Now what happens? – NightFury Sep 09 '13 at 05:44
  • Don't know which method you mean to put it in so tried both cellForRowAtIndexPath and didSelectRowAtIndexPath (separately) and either one is removing all styling. – PaulMrG Sep 09 '13 at 05:54
  • look at this. I think this is what you need... http://stackoverflow.com/questions/18425265/how-to-add-a-custom-cell-on-select-event-of-uitableview-in-accordion-style – NightFury Sep 09 '13 at 06:02
0

In cellForRowAtIndexPath method add this line before returning your cell instance:

[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
Mrunal
  • 13,982
  • 6
  • 52
  • 96
  • 1
    Same as what I've tried from @anum90 it removes all styling instead of using my custom styling – PaulMrG Sep 09 '13 at 06:06
0

Starting with the workaround as suggested by @Andrey Gordeev I extended that to include removing the style layer once the cell is deselected.

I still need to use my custom cell for other reasons, but I have shown it as a standard cell below so anyone can use it

To apply your own style if you have to call

[myTable selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];

To add a gradient layer when selecting a cell

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = (UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath];

    CAGradientLayer *gradient = [CAGradientLayer layer];
    [gradient setFrame:[cell.contentView bounds]];
    [gradient setColors:[NSArray arrayWithObjects:(id)[[UIColor colorWithWhite:0.70 alpha:1.0] CGColor], (id)[[UIColor colorWithWhite:0.50 alpha:1.0] CGColor], nil]];
    [[cell.contentView layer] insertSublayer:gradient atIndex:0];
    // The next line makes a reference to the gradient layer for selection and removal later on
    [[cell.contentView layer] setValue:gradient forKey:@"GradientLayer"];

    ...

}

To remove the gradient layer when deselecting a cell

-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = (UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath];

    // Get the layer using the reference to it
    CALayer *gradientLayer = [[cell.contentView layer] valueForKey:@"GradientLayer"];
    [gradientLayer removeFromSuperlayer];
    [[cell.contentView layer] setValue:nil forKey:@"GradientLayer"];

    ...

}
PaulMrG
  • 1,822
  • 2
  • 16
  • 20
0

I had a similar issue, here is how I solved it :

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

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CELL_NAME];...

      // Cell custom style
UIImage *background             = [self cellBackgroundForRowAtIndexPath:indexPath];
UIImageView *cellBackgroundView = [[UIImageView alloc] initWithImage:background];
cellBackgroundView.image        = background;
cell.backgroundView             = cellBackgroundView;

UIImage *selectedBackground             = [self cellBackgroundForRowAtIndexPath:indexPath];
UIImageView *cellselectedBackgroundView = [[UIImageView alloc] initWithImage:background];
cellBackgroundView.image                = selectedBackground;
cell.selectedBackgroundView             = cellselectedBackgroundView;
LudoZik
  • 917
  • 1
  • 8
  • 20