Custom selected background color for group UITableViewCell

Is there a good way to set a custom selected background color in a cell in a grouped table? I do not want to take four images and keep track of which one is suitable for which cell.

I would use the UITableViewCell backgroundViewand properties selectedBackgroundView, but they break the rounded corners of the grouped table view.

Now I have a subclass UITableViewCellthat overrides -setHighlighted:animated:and -setSelected:animated:to switch cells backgroundColor. This works great, except that it is not animated, although it backgroundColoris an animated property, and changes to it are completed when called -beginAnimations:context:and -commitAnimations, if necessary.

+5
source share
2 answers

You might want to take a look at Matt Gallagher's TableDesignRevisited . I really like the way it creates and manages UITableViews.

The only other solution I know for changing the selection style of grouped table cells is to use 4 backgrounds (top, bottom, middle, top and bottom) and apply them based on the position of the cell in the table.

0
source

I developed the following solution:

Subclass of UITableViewCell. Define background colors for the selected and unselected cells, for example:

#define SELECTED_BACKGROUND_COLOR [UIColor redColor]
#define NOT_SELECTED_BACKGROUND_COLOR [UIColor whiteColor]

(You can also create custom properties)

UITableViewCell. , , @lemnar, backgroundColor . , NSTimers, .

- (void) mixBackgroundColorWithSelectedColorMultiplier:(NSNumber *)multiplier 
{
    CGFloat *selComponents = (CGFloat *) CGColorGetComponents(SELECTED_BACKGROUND_COLOR.CGColor);
    CGFloat *notSelComponents = (CGFloat *) CGColorGetComponents(NOT_SELECTED_BACKGROUND_COLOR.CGColor);

    if((CGColorGetNumberOfComponents(SELECTED_BACKGROUND_COLOR.CGColor) == 2)
    {
        selComponents[2] = selComponents[1] = selComponents[0];
    }

    if((CGColorGetNumberOfComponents(NOT_SELECTED_BACKGROUND_COLOR.CGColor) == 2)
    {
        notSelComponents[2] = notSelComponents[1] = notSelComponents[0];
    }

    CGFloat m = [multiplier floatValue];
    self.backgroundColor = [UIColor colorWithRed:(notSelComponents[0]) * (1.0 - m) + (selComponents[0]) * m
                                                                                 green:(notSelComponents[1]) * (1.0 - m) + (selComponents[1]) * m
                                                                                    blue:(notSelComponents[2]) * (1.0 - m) + (selComponents[2]) * m
                                                                                 alpha:1.0];
    [self setNeedsDisplay];
}

- (void) setSelected:(BOOL)selected animated:(BOOL)animated 
{
    if(selected) 
    {
        if(animated)
        {
            [self performSelector:@selector(mixBackgroundColorWithSelectedColorMultiplier:) withObject:[NSNumber numberWithFloat:0.2] afterDelay:0.08];
            [self performSelector:@selector(mixBackgroundColorWithSelectedColorMultiplier:) withObject:[NSNumber numberWithFloat:0.4] afterDelay:0.16];
            [self performSelector:@selector(mixBackgroundColorWithSelectedColorMultiplier:) withObject:[NSNumber numberWithFloat:0.6] afterDelay:0.24];
            [self performSelector:@selector(mixBackgroundColorWithSelectedColorMultiplier:) withObject:[NSNumber numberWithFloat:0.8] afterDelay:0.32];
            [self performSelector:@selector(mixBackgroundColorWithSelectedColorMultiplier:) withObject:[NSNumber numberWithFloat:1.0] afterDelay:0.40];
        }
        else
        {
            [self mixBackgroundColorWithSelectedColorMultiplier:[NSNumber numberWithFloat:1.0]];
        }
    }
    else
    {
        if(animated)
        {
            [self performSelector:@selector(mixBackgroundColorWithSelectedColorMultiplier:) withObject:[NSNumber numberWithFloat:0.8] afterDelay:0.08];
            [self performSelector:@selector(mixBackgroundColorWithSelectedColorMultiplier:) withObject:[NSNumber numberWithFloat:0.6] afterDelay:0.16];
            [self performSelector:@selector(mixBackgroundColorWithSelectedColorMultiplier:) withObject:[NSNumber numberWithFloat:0.4] afterDelay:0.24];
            [self performSelector:@selector(mixBackgroundColorWithSelectedColorMultiplier:) withObject:[NSNumber numberWithFloat:0.2] afterDelay:0.32];
            [self performSelector:@selector(mixBackgroundColorWithSelectedColorMultiplier:) withObject:[NSNumber numberWithFloat:0.0] afterDelay:0.40];
        }
        else
        {
            [self mixBackgroundColorWithSelectedColorMultiplier:[NSNumber numberWithFloat:0.0]];
        }
    }
    [super setSelected:selected animated:animated];
}

- (void) setHighlighted:(BOOL)highlighted animated:(BOOL)animated 
{
    if(highlighted) 
    {
        if(animated)
        {
            [self performSelector:@selector(mixBackgroundColorWithSelectedColorMultiplier:) withObject:[NSNumber numberWithFloat:0.2] afterDelay:0.08];
            [self performSelector:@selector(mixBackgroundColorWithSelectedColorMultiplier:) withObject:[NSNumber numberWithFloat:0.4] afterDelay:0.16];
            [self performSelector:@selector(mixBackgroundColorWithSelectedColorMultiplier:) withObject:[NSNumber numberWithFloat:0.6] afterDelay:0.24];
            [self performSelector:@selector(mixBackgroundColorWithSelectedColorMultiplier:) withObject:[NSNumber numberWithFloat:0.8] afterDelay:0.32];
            [self performSelector:@selector(mixBackgroundColorWithSelectedColorMultiplier:) withObject:[NSNumber numberWithFloat:1.0] afterDelay:0.40];
        }
        else
        {
            [self mixBackgroundColorWithSelectedColorMultiplier:[NSNumber numberWithFloat:1.0]];
        }
    }
    else
    {
        if(animated)
        {
            [self performSelector:@selector(mixBackgroundColorWithSelectedColorMultiplier:) withObject:[NSNumber numberWithFloat:0.8] afterDelay:0.08];
            [self performSelector:@selector(mixBackgroundColorWithSelectedColorMultiplier:) withObject:[NSNumber numberWithFloat:0.6] afterDelay:0.16];
            [self performSelector:@selector(mixBackgroundColorWithSelectedColorMultiplier:) withObject:[NSNumber numberWithFloat:0.4] afterDelay:0.24];
            [self performSelector:@selector(mixBackgroundColorWithSelectedColorMultiplier:) withObject:[NSNumber numberWithFloat:0.2] afterDelay:0.32];
            [self performSelector:@selector(mixBackgroundColorWithSelectedColorMultiplier:) withObject:[NSNumber numberWithFloat:0.0] afterDelay:0.40];
        }
        else
        {
            [self mixBackgroundColorWithSelectedColorMultiplier:[NSNumber numberWithFloat:0.0]];
        }
    }
    [super setHighlighted:highlighted animated:animated];
}
-1

All Articles