简体   繁体   English

突出显示表格视图单元格不会使用Objective-C突出显示表格视图单元格的子视图

[英]Highlighting table view cell will not highlight subviews of table view cell using objective-C

I had table view with customised cell. 我有带有自定义单元格的表格视图。 In each row of the table view cell, there is subview with gradient colour and each row's background colour is different from subview's colour. 在表格视图单元格的每一行中,都有带有渐变颜色的子视图,并且每行的背景颜色与子视图的颜色不同。

when I tap or touch each table view cell, then it is only highlighting background of tableview cell and not highlighting the subview. 当我点击或触摸每个表格视图单元格时,它只是突出显示表格视图单元格的背景,而不突出显示子视图。 All the cell contents are not highlighted with green colour. 所有单元格内容均未用绿色突出显示。

  1. I tried removing gradient layer of subview and added background colour to it, then all the cell contents are highlighting but with gradient layer it is not highlighting. 我尝试删除子视图的渐变层并向其添加背景色,然后所有单元格内容均突出显示,但渐变层未突出显示。

  2. Also I need to save the status, whether it is highlighted or not, how can i achieve this? 我还需要保存状态,无论是否突出显示,如何实现?

Here is the screen shot, 这是屏幕截图,

在此处输入图片说明

in cellForRowAtIndexPath method 在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];                       

guide me to solve this issue. 指导我解决这个问题。

You have to override the setHighlighted method in your custom tableview cell, and set the highlight on the subviews as well. 您必须在自定义表格视图单元格中覆盖setHighlighted方法,并在子视图上同时设置突出显示。

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
  [super setHighlighted:highlighted animated:animated];
  self.customView.backgroundColor = [UIColor redColor];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM