添加CAGradientLayer子层时UITableViewCell不拾取UITableViewCellSelectionStyleBlue

时间:2011-07-21 22:34:56

标签: iphone objective-c ios cocoa-touch core-graphics

我有一个UITableViewCell,我已经使用CAGradientLayer添加了渐变。这样工作正常,但表格单元格在选中时不会变为蓝色,即使在将其选择样式设置为UITableViewCellSelectionStyleBlue之后也是如此。如果我不添加渐变图层,它可以正常工作。

有没有办法让这些项目一起工作?

这是我在cellForRowAtIndexPath中的代码:

    //cell gradient        
    CAGradientLayer *gradient = [CAGradientLayer layer];
    gradient.frame = CGRectMake(10, 0, 300, 50);
    gradient.colors = [NSArray arrayWithObjects:
                       (id)[[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1] CGColor],
                       (id)[[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1] CGColor],
                       (id)[[UIColor colorWithRed:245/255.0 green:245/255.0 blue:245/255.0 alpha:1] CGColor],
                       (id)[[UIColor colorWithRed:247/255.0 green:247/255.0 blue:247/255.0 alpha:1] CGColor],
                       nil];
    gradient.locations = [NSArray arrayWithObjects:
                          (id)[NSNumber numberWithFloat:0.00],
                          (id)[NSNumber numberWithFloat:0.50],
                          (id)[NSNumber numberWithFloat:0.51],
                          (id)[NSNumber numberWithFloat:1.0],                                                                                          
                          nil];
    [cell.layer insertSublayer:gradient atIndex:0];


    //bring back rounded corners by creating a masklayer 
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:gradient.bounds byRoundingCorners:UIRectCornerBottomRight|UIRectCornerBottomLeft cornerRadii:CGSizeMake(8, 8)];
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.frame = gradient.bounds;
    maskLayer.path = maskPath.CGPath;
    gradient.mask = maskLayer;

    //add the cell shadow
    cell.layer.shadowColor = [[UIColor blackColor] CGColor];
    cell.layer.shadowRadius = 3;
    cell.layer.shadowOpacity = 0.5;
    cell.layer.shadowOffset = CGSizeMake(0, 0);

    cell.selectionStyle = UITableViewCellSelectionStyleBlue;      

1 个答案:

答案 0 :(得分:2)

看起来你可能会将渐变放在视图上,当它被选中时会发生变化 - UITableViewCellSelectionStyleBlue视图可能会出现,但你只是看不到它(测试它的方法只有渐变覆盖部分你的细胞 - 如果另一部分在选择时改变了颜色,那么这就是问题)。

如果这是问题,那么当通过子类化UITableViewCell选择单元格时,您可以为单元格做自己的自定义绘图,或者只要选择单元格就可以使渐变消失(并在取消选择单元格时重新出现)。