博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
分组圆角TableView
阅读量:7036 次
发布时间:2019-06-28

本文共 2715 字,大约阅读时间需要 9 分钟。

2016.4.6 更新 增加边框功能,调用的时候设置边框线宽度与颜色即可,具体见 效果预览:

2016.2.4 更新 重新修改了代码,一句话即可使用,具体见


自己写的一个实现TableView的每个分组四角是圆角。 效果如下:

我是通过自定义TableViewCell来实现这个功能的,外部引用的时候,通过判断该cellsection中所处的位置来判断其圆角类型。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {    static NSString *kCellID = @"CELLID";    KKRoundCornerCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellID];    if (!cell) {        cell = [[KKRoundCornerCell alloc] initWithCornerRadius:10.0f Style:UITableViewCellStyleDefault reuseIdentifier:kCellID];    }        if ([tableView numberOfRowsInSection:indexPath.section] == 1) {        cell.roundCornerType = KKRoundCornerCellTypeSingleRow;    } else if (indexPath.row == 0) {        cell.roundCornerType = KKRoundCornerCellTypeTop;    } else if (indexPath.row == [tableView numberOfRowsInSection:indexPath.section] - 1) {        cell.roundCornerType = KKRoundCornerCellTypeBottom;    } else {        cell.roundCornerType = KKRoundCornerCellTypeDefault;    }        cell.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 256.0                                          green:arc4random() % 256 / 256.0                                           blue:arc4random() % 256 / 256.0                                          alpha:1.0];    cell.textLabel.text = [NSString stringWithFormat:@"第%ld组第%ld行", indexPath.section + 1, indexPath.row + 1];        return cell;}复制代码

圆角是使用贝赛尔曲线画出来然后使用layer.mask实现。

- (void)setFrame:(CGRect)frame {    [super setFrame:frame];    UIBezierPath *path;    switch (_roundCornerType) {        case KKRoundCornerCellTypeTop: {            path = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(_cornerRadius, _cornerRadius)];            break;        }                case KKRoundCornerCellTypeBottom: {            path = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(_cornerRadius, _cornerRadius)];            break;        }                    case KKRoundCornerCellTypeSingleRow: {            path = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(_cornerRadius, _cornerRadius)];            break;        }                case KKRoundCornerCellTypeDefault:        default: {            self.layer.mask = nil;            return;        }    }    CAShapeLayer *maskLayer = [CAShapeLayer layer];    maskLayer.frame = self.bounds;    maskLayer.path = path.CGPath;    self.layer.mask = maskLayer;    }复制代码

代码其实很简单,操作过程中发现系统刚创建出来的TableViewCell的宽度默认是320,要把mask写在setFrame:里面。

代码已上传至

转载地址:http://ygyal.baihongyu.com/

你可能感兴趣的文章
[LeetCode] 662. Maximum Width of Binary Tree
查看>>
小李飞刀:python你慢点飞,我的脑子还在后面追
查看>>
请描述一下cookies,sessionStorage和localStorage的区别?
查看>>
YOLO目标检测模型原理介绍
查看>>
Java IO框架总揽--FileOutputStream源码解读
查看>>
如何使用ABAP代码反序列化JSON字符串成ABAP结构
查看>>
Vue 使用History记录上一页面的数据
查看>>
小程序开发小结
查看>>
手把手教你从零开始搭建SpringBoot后端项目框架
查看>>
Javascript-函数节流与函数防抖
查看>>
浅谈JavaScript中的面向对象
查看>>
etcd管理,证书配置,扩展,迁移恢复,带证书扩展节点
查看>>
LeetCode65. Valid Number -- 判断合法数字
查看>>
【大数据实践】游戏事件处理系统(1)——事件收集-filebeat
查看>>
nodejs微服务解决方案
查看>>
ionic3学习之集成 ngx-translate
查看>>
微信小程序函数调用监控
查看>>
linux安装mysql5.5版本,并配置相关mysql负载。
查看>>
RocketMQ快速入门
查看>>
解决 Python3 下 print 时出现 UnicodeEncodeError 的问题
查看>>