美文网首页iOS开发技巧iOS开发学习文档
关于UITableView-给UITableViewCell添加

关于UITableView-给UITableViewCell添加

作者: 九色鹿的女孩 | 来源:发表于2020-06-11 11:47 被阅读0次

一:首先给UIView添加一个扩展,加入添加圆角的方法
OC实现

//顶部所有圆角
- (void)drawTopCornerRadius:(NSInteger)radius {
    // 任意圆角
    CGPathRef path = [UIBezierPath bezierPathWithRoundedRect:self.bounds
    byRoundingCorners:UIRectCornerTopRight|UIRectCornerTopLeft cornerRadii:CGSizeMake(radius, radius)].CGPath;
    CAShapeLayer *lay = [CAShapeLayer layer];
    lay.path = path;
    self.layer.mask = lay;
}

// 顶部所有圆角
- (void)drawBottomCornerRadius:(NSInteger)radius {
    // 任意圆角
    CGPathRef path = [UIBezierPath bezierPathWithRoundedRect:self.bounds
    byRoundingCorners:UIRectCornerBottomLeft|UIRectCornerBottomRight cornerRadii:CGSizeMake(radius, radius)].CGPath;
    CAShapeLayer *lay = [CAShapeLayer layer];
    lay.path = path;
    self.layer.mask = lay;
}
typedef NS_OPTIONS(NSUInteger, UIRectCorner) {
    UIRectCornerTopLeft     = 1 << 0,
    UIRectCornerTopRight    = 1 << 1,
    UIRectCornerBottomLeft  = 1 << 2,
    UIRectCornerBottomRight = 1 << 3,
    UIRectCornerAllCorners  = ~0UL
};
Corners可以选择设置对应的哪个圆角

二:在- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath 方法实现,添加圆角的逻辑

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) { // 第一行顶部添加
        [cell drawTopCornerRadius:20];
    } else if(indexPath.row == 最后一行) { //最后一行底部添加
        [cell drawBottomCornerRadius:20];
    }
}

如图:就这样完美的设置好啦


截屏2020-06-11上午11.45.45.png

相关文章

网友评论

    本文标题:关于UITableView-给UITableViewCell添加

    本文链接:https://www.haomeiwen.com/subject/xcxbtktx.html