是否有一种在分组表格视图单元格上设置自定义所选背景颜色的好方法?我不想制作四张图像并跟踪哪一张适合哪个细胞。
我会使用UITableViewCell
的{{1}}和backgroundView
属性,但它们会破坏分组的表格视图样式的圆角。
现在,我有一个selectedBackgroundView
子类,它会覆盖UITableViewCell
和-setHighlighted:animated:
来切换单元格的-setSelected:animated:
。虽然backgroundColor
是一个可动画的属性,但它的动画效果非常完美,但对它的更改会在适当时调用backgroundColor
和-beginAnimations:context:
。
答案 0 :(得分:0)
你可能想看看Matt Gallagher的TableDesignRevisited。我非常喜欢他构建和管理UITableViews的方式。
我知道改变分组tableview单元格选择方式的唯一其他解决方案是使用4个背景(顶部,底部,中间,顶部和底部),并根据表格中的单元格位置应用它们。
答案 1 :(得分:-1)
我设计了以下解决方案:
UITableViewCell子类。定义所选单元格和未选定单元格的背景颜色,例如:
#define SELECTED_BACKGROUND_COLOR [UIColor redColor]
#define NOT_SELECTED_BACKGROUND_COLOR [UIColor whiteColor]
(您也可以创建特别设计的属性)
然后,您必须覆盖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];
}