1. 程式人生 > >TableViewCell 自定義點選的高亮顏色

TableViewCell 自定義點選的高亮顏色

在工作中遇到自定義cell點選效果的需求,系統的grayType色值與UI給的不一致。可以採用下面的方法實現。

1:在自定義的UITableViewCell的 -(void)aweakFromNib{}的方法中加入。

- (void)awakeFromNib {
    [super awakeFromNib];
    CGRect rect = CGRectMake(0, 0, self.width, self.height);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    //高亮顏色值
    UIColor *selectedColor = [UIColor redColor];
    CGContextSetFillColorWithColor(context, [selectedColor CGColor]);
    CGContextFillRect(context, rect);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    self.selectedBackgroundView = [[UIImageView alloc] initWithImage:image];
    
}