我已经将左下角和右下角半径应用于我的UITableViewCell
,它工作正常,但宽度cell
减少containerView
了cell
。我在那个中应用了约束。这背后的原因是什么?
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
RateChartTableViewCell *cell1 = (RateChartTableViewCell *)cell;
if(indexPath.section == 0 && indexPath.row == 4)
{
[tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
UIBezierPath *maskPath = [UIBezierPath
bezierPathWithRoundedRect:cell1.containerView.bounds byRoundingCorners:( UIRectCornerBottomLeft | UIRectCornerBottomRight) cornerRadii:CGSizeMake(10.0, 10.0)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = tableView.bounds;
maskLayer.path = maskPath.CGPath;
cell1.containerView.layer.mask = maskLayer;
cell1.containerView.clipsToBounds = YES;
}
}
这是因为单元格的大小已更改但maskLayer
仍保持旧大小。在我看来,要修复它,每次更改单元格的大小时,请删除并maskLayer
再次添加。
RateChartTableViewCell
@interface RateChartTableViewCell : UITableViewCell
@property(nonatomic, strong) CAShapeLayer* maskLayer;
... other properties
@end
@implementation RateChartTableViewCell
- (void)configureBorders {
UIBezierPath* maskPath =
[UIBezierPath bezierPathWithRoundedRect:self.bounds
byRoundingCorners:(UIRectCornerBottomLeft |
UIRectCornerBottomRight)
cornerRadii:CGSizeMake(10.0, 10.0)];
_maskLayer = [[CAShapeLayer alloc] init];
_maskLayer.frame = self.bounds;
_maskLayer.path = maskPath.CGPath;
self.layer.mask = _maskLayer;
self.clipsToBounds = YES;
self.backgroundColor = UIColor.redColor;
}
- (void)layoutSubviews {
[super layoutSubviews];
[_maskLayer removeFromSuperlayer];
[self configureBorders];
}
- (void)prepareForReuse {
[super prepareForReuse];
[_maskLayer removeFromSuperlayer];
}
@end
视图控制器
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
RateChartTableViewCell* cell = // Initialize cell
// Do other things
if (indexPath.section == 0 && indexPath.row == 4) {
[tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone]; // You can move this line to |viewDidLoad|
[cell configureBorders];
}
return cell;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句