3

Is it possible to exclude certain subviews in a UITableViewCell from being highlighted or to change their highlight implementation?

I added an orange warning label to the UITableViewCell for certain occassions and it highlights properly with the rest of the cell, which is good. However, when the cells stops being highlighted in an animated way, you first see the background of the warning label become white and then flash to orange. I would either like it to be excluded from being highlighted at all, or to change it's highlight implementation so it animates back to orange properly.

SpacyRicochet
  • 2,269
  • 2
  • 24
  • 39
  • So far I have figured out that overriding setHighlight:animated: and returning immediately cancels out starting highlighted. But the highlight when I press the cell still turns the lable blue. – SpacyRicochet Nov 29 '11 at 10:33
  • There has been similar issues elsewhere as well. I posted an answer here http://stackoverflow.com/a/36886209/81388. – Peymankh Apr 27 '16 at 10:42

4 Answers4

3

It doesn't seem to be possible to exclude views from being highlighted if the superview calls for it. However, overriding their setHighlighted: can give the same result if you just stop any highlighting behavior there or change it to your specifications.

SpacyRicochet
  • 2,269
  • 2
  • 24
  • 39
1

In iOS 6, you can use 2 methods in UITableViewDelegate to customise highlighting for the cell, and also it subviews:

- (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    [cell setBackgroundColor:[UIColor lightGrayColor]];
}

- (void)tableView:(UITableView *)tableView didUnhighlightRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    [UIView animateWithDuration:0.5f animations:^{
        [cell setBackgroundColor:[UIColor whiteColor]];
    }];
}

I did not show the code for changing the subviews.

But you can do similarly like the way you configure for each cell, using cell viewWithTag to access the subview, then change the background or any other properties.

samwize
  • 25,675
  • 15
  • 141
  • 186
0

You cannot remove certain subviews from animating, but you can change some behaviors as follow:

Avoid the UITableViewCell from doing a highlighting animation like this:

cell.selectionStyle = UITableViewCellSelectionStyleNone;

When a cell is selected, do manual changes to background like this example:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    [cell setBackgroundColor:[UIColor purpleColor]];

    [UIView animateWithDuration:0.5f animations:^{
            [cell setBackgroundColor:[UIColor clearColor]];
    }];

}

I had a simple UITableView and this worked as I needed.

Krynble
  • 614
  • 7
  • 18
0

in order to have a custom highlight behavior on a table view cell, override setHighlighted method and parse highlighted:false in to the super method, and customize specify which views should change color based on highlight property below

Sajidh Zahir
  • 526
  • 4
  • 13