【问题标题】:UITableviewCell Bottom Left & Right corner radiusUITableviewCell 左下角和右下角半径
【发布时间】:2018-06-13 07:16:29
【问题描述】:

我已将左下角和右下角半径应用于我的UITableViewCell 它工作正常,但cell 的宽度减少了。我在cellcontainerView 中应用了约束。这背后的原因是什么?

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

【问题讨论】:

    标签: ios objective-c xcode uitableview


    【解决方案1】:

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

    【讨论】:

    • 它可以工作,但是当我在那之后滚动tableview
    • 使用celForRowAtIndexPath更新了我的答案
    • 是一样的。我需要滚动tableView
    • cornerRadius 发生,但 cell 宽度小于应有的宽度。
    • 我认为这是containerView 的问题。试试我更新的答案
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2016-06-08
      • 1970-01-01
      • 2020-10-27
      • 2023-03-25
      • 2013-12-22
      • 2017-01-18
      • 2021-08-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多