我有自定义单元格的表视图。在表格视图单元格的每一行中,都有带渐变颜色的子视图,每行的背景颜色与子视图的颜色不同。
当我点击或触摸每个表格视图单元格时,它只突出显示tableview单元格的背景而不突出显示子视图。所有单元格内容均未以绿色突出显示。
我尝试删除子视图的渐变层并为其添加背景颜色,然后所有单元格内容都突出显示,但是渐变图层没有突出显示。
此外,我需要保存状态,无论是否突出显示,我该如何实现?
这是屏幕截图,
in cellForRowAtIndexPath方法
UIView *selection_color = [[UIView alloc] init];
selection_color.backgroundColor = [UIColor greenColor];
cell.selectedBackgroundView = selection_color;
UIView *view = [UIView alloc] init];
view.layer.cornerRadius = 10;
view.layer.masksToBounds = YES;
view.layer.borderColor = [[UIColor colorWithRed:197/255.0 green:197.0/255.0 blue:197.0/255.0 alpha:1.0] CGColor];
view.layer.borderWidth = 1.0f;
view.tag = 10;
[cell.contentView addSubview: view];
NSArray *gradient_colors = [NSArray arrayWithObjects:(id)[UIColor redColor].CGColor, (id)[UIColor whiteColor].CGColor, nil];
NSArray *gradient_locations = [NSArray arrayWithObjects:[NSNumber numberWithInt:0.0],[NSNumber numberWithInt:1.0], nil];
CAGradientLayer *gradientLayer = [CAGradientLayer layer];
gradientLayer.colors = gradient_colors;
gradientLayer.locations = gradient_locations;
gradientLayer.masksToBounds = YES;
gradientLayer.frame = view.layer.bounds;
[view.layer addSublayer:gradientLayer];
指导我解决这个问题。
答案 0 :(得分:1)
您必须覆盖自定义tableview单元格中的setHighlighted方法,并在子视图上设置突出显示。
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
[super setHighlighted:highlighted animated:animated];
self.customView.backgroundColor = [UIColor redColor];
}