简体   繁体   中英

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

 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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