UITableView cell圆角

在UITableView里判断第一行和最后一行

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"NewsTableViewCell";
    PersonTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[PersonTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
   
    PersonModel *model = _tvData[indexPath.row];
    
    BOOL first = NO;
    BOOL last = NO;
    
    if (indexPath.row ==0) {
        first = YES;
    }
    
    if(indexPath.row == _tvData.count -1){
        last = YES;
    }
    [cell setItem:model isFirst:first isLast:last];

    return cell;
}

在自定义Cell里画圆角

-(void)setItem:(PersonModel *)model isFirst:(BOOL)isFirst isLast:(BOOL)isLast{
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:_bgView.layer.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(0, 0)];

    if (isFirst && isLast) {
        maskPath = [UIBezierPath bezierPathWithRoundedRect:_bgView.layer.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(SCREEN_3SPACE(10), SCREEN_3SPACE(10))];

    }else{
        if (isFirst) {
            maskPath = [UIBezierPath bezierPathWithRoundedRect:_bgView.layer.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(SCREEN_3SPACE(10), SCREEN_3SPACE(10))];
        }
        if (isLast) {
            maskPath = [UIBezierPath bezierPathWithRoundedRect:_bgView.layer.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(SCREEN_3SPACE(10), SCREEN_3SPACE(10))];

        }
        
    }
    CAShapeLayer *maskLayer = [CAShapeLayer new];
    maskLayer.frame = _bgView.layer.bounds;
    maskLayer.path = maskPath.CGPath;
    _bgView.layer.mask = maskLayer;
    

    _nameLabel.text = model.name;
    _phoneLabel.text = model.phone;
    _timeLabel.text = [NSString stringWithFormat:@"注册时间:%@",model.date];
    
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。