2014-09-05 49 views
0

我已经创建,创建一个更小的单元格的外观和空间,它们之间的错觉自定义单元格的外观:的UITableView:抵消selectedBackgroundView

// SETTING UP A UIView IN ORDER TO CREATE ILLUSION OF SEPARATION BETWEEN CELLS 
cell.contentView.backgroundColor = [UIColor clearColor]; 
UIView *clearRoundedCornerView = [[UIView alloc] initWithFrame:CGRectMake(10,10,310,110)]; 
clearRoundedCornerView.backgroundColor = [UIColor clearColor]; 
clearRoundedCornerView.layer.masksToBounds = YES; 
clearRoundedCornerView.layer.cornerRadius = 3.0; 
clearRoundedCornerView.layer.shadowOffset = CGSizeMake(-1, 1); 
clearRoundedCornerView.layer.shadowOpacity = 0.5; 

// Set up Mask with 2 rounded corners 
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:clearRoundedCornerView.bounds byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerBottomLeft) cornerRadii:CGSizeMake(20.0, 20.0)]; 
CAShapeLayer *cornerMaskLayer = [CAShapeLayer layer]; 
[cornerMaskLayer setPath:path.CGPath]; 
clearRoundedCornerView.layer.mask = cornerMaskLayer; 

// Make a transparent, stroked laker which will display the stroke 
CAShapeLayer *strokeLayer = [CAShapeLayer layer]; 
strokeLayer.path = path.CGPath; 
strokeLayer.fillColor = [UIColor clearColor].CGColor; 
strokeLayer.strokeColor = [UIColor colorWithRed:(194/255.0) green:(77/255.0) blue:(1/255.0) alpha:1].CGColor; 
strokeLayer.lineWidth = 5.0; 

// Transparent view that will contain the stroke layer 
UIView *strokeView = [[UIView alloc] initWithFrame:clearRoundedCornerView.bounds]; 
strokeView.userInteractionEnabled = NO; // in case your container view contains controls 
[strokeView.layer addSublayer:strokeLayer]; 

[clearRoundedCornerView addSubview:strokeView]; 


[cell.contentView addSubview:clearRoundedCornerView]; 

在下面的代码我试图修改selectedBackgroundView匹配我创建的视图。我遇到的问题是X Y坐标不会改变,而selectedBackgroundView保持固定在单元格的左上角。

- (void)installSelectedBackgroundView{ 

UIView *bgCustomView = [[UIView alloc] initWithFrame:CGRectMake(10,10,310,110)]; 
bgCustomView.backgroundColor = [UIColor lightGrayColor]; 
bgCustomView.layer.masksToBounds = YES; 
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:bgCustomView.bounds byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerBottomLeft) cornerRadii:CGSizeMake(20.0, 20.0)]; 
CAShapeLayer *cornerMaskLayer = [CAShapeLayer layer]; 
[cornerMaskLayer setPath:path.CGPath]; 
bgCustomView.layer.mask = cornerMaskLayer; 
self.selectedBackgroundView = bgCustomView; 
[self setSelected:YES animated:YES]; 
} 

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if (self) { 
     [self installSelectedBackgroundView]; 
    } 
    return self; 
} 

- (void)awakeFromNib { 
    [super awakeFromNib]; 
    [self installSelectedBackgroundView]; 
} 

不知道为什么这不起作用。在bgCustomView中更改X Y似乎没有任何成就。

谢谢。

回答

0

您需要更改installSelectedBackgroundView中路径的原点。正如你已经注意到的,bgCustomView的起源被忽略;这是因为selectedBackgroundView,它将具有与细胞相同的来源。所以,相反,你需要抵消掩模图层路径的原点,

UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectOffset(bgCustomView.bounds, 10, 10) byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerBottomLeft) cornerRadii:CGSizeMake(20.0, 20.0)]; 
+0

这工作完美。谢谢。 – Mountainbrussell 2014-09-08 22:32:49