Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 为分组的UITableViewCell自定义选定的背景色_Iphone_Objective C_Uitableview - Fatal编程技术网

Iphone 为分组的UITableViewCell自定义选定的背景色

Iphone 为分组的UITableViewCell自定义选定的背景色,iphone,objective-c,uitableview,Iphone,Objective C,Uitableview,有没有一种好方法可以在分组的表视图单元格上设置自定义选定的背景色?我不想制作四个图像,并跟踪哪个图像适合哪个单元格 我会使用UITableViewCell的backgroundView和selectedBackgroundView属性,但它们打破了分组表视图样式的圆角 现在,我有一个UITableViewCell子类,它覆盖-setHighlighted:animated:和-setSelected:animated:来切换单元格的背景色。这工作非常完美,除了它没有动画,即使background

有没有一种好方法可以在分组的表视图单元格上设置自定义选定的背景色?我不想制作四个图像,并跟踪哪个图像适合哪个单元格

我会使用
UITableViewCell
backgroundView
selectedBackgroundView
属性,但它们打破了分组表视图样式的圆角


现在,我有一个
UITableViewCell
子类,它覆盖
-setHighlighted:animated:
-setSelected:animated:
来切换单元格的
背景色。这工作非常完美,除了它没有动画,即使
backgroundColor
是一个可动画的属性,对它的更改被包装在调用
-beginAnimations:context:
-commitAnimations
中(如果合适)。

您可能想看看Matt Gallagher的作品。我真的很喜欢他构建和管理UITableView的方式


我所知道的更改分组tableview单元格选择样式的唯一其他解决方案是使用4个背景(顶部、底部、中部、顶部和底部),并根据单元格在表格中的位置应用它们。

我设计了以下解决方案:

子类UITableViewCell。定义选定和未选定单元格的背景颜色,例如:

#define SELECTED_BACKGROUND_COLOR [UIColor redColor]
#define NOT_SELECTED_BACKGROUND_COLOR [UIColor whiteColor]
(也可以创建专门设计的属性)

然后,必须重写UITableViewCell的两个方法。 我还介绍了简单的动画,因为正如@lemnar所说,backgroundColor属性是不可动画的。如果使用NSTimer,动画可能会更好,但这会使代码更长

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

我看了马特·加拉赫的帖子,但他向牢房传递了关于表格的信息,我并不想这么做,也就是说,我要寻找的“优雅”是一个位置不可知的牢房。我最终使用了<代码> CAShapeLayer <代码>支持的视图,用于单元格的<代码> SealDeBeaBoeVIEW ,但我认为你是正确的,唯一实现这一点的方法是关于一个单元格是否在顶部、底部、中间的知识,或者是节中唯一的单元格。关于此方法的一些反馈?它不工作,并且第一个函数未编译(需要重构才能编译)