UITableviewCell左下角&右角半径

时间:2018-06-13 07:16:29

标签: ios objective-c xcode uitableview

我已将左下角和右下角半径应用于我的UITableViewCell 并且它工作正常但cell的宽度减小了。我在containerView的{​​{1}}中应用了约束。这背后的原因是什么?

enter image description here

cell

2 个答案:

答案 0 :(得分:0)

这是因为单元格的大小已更改,但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

的ViewController

- (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;
}

答案 1 :(得分:0)

maskLayer.frame = tableView.bounds;也许错了。你可以改变它: maskLayer.frame = cell1.bounds; 希望它会对你有所帮助。