func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
let radius = 8.0 //圆角
cell.backgroundColor = .clear
let normalLayer = CAShapeLayer()
let selectLayer = CAShapeLayer()
let bounds = cell.bounds.insetBy(dx:10.0, dy:0) //cell 左右缩进距离
let rowNum = tableView.numberOfRows(inSection: indexPath.section)
var bezierPath:UIBezierPath
if(rowNum==1) {
bezierPath = UIBezierPath(roundedRect: bounds, byRoundingCorners: .allCorners, cornerRadii:CGSize(width: radius, height: radius))
}else{
if(indexPath.row==0) {
bezierPath = UIBezierPath(roundedRect: bounds, byRoundingCorners:[UIRectCorner.topLeft,UIRectCorner.topRight], cornerRadii:CGSize(width: radius, height: radius))
}else if(indexPath.row==rowNum-1) {
bezierPath = UIBezierPath(roundedRect: bounds, byRoundingCorners: [UIRectCorner.bottomLeft,UIRectCorner.bottomRight], cornerRadii:CGSize(width: radius, height: radius))
}else{
bezierPath = UIBezierPath(rect: bounds)
}
}
normalLayer.path = bezierPath.cgPath
selectLayer.path = bezierPath.cgPath
let nomarBgView = UIView(frame: bounds)
normalLayer.fillColor = UIColor(51, 51, 64).cgColor
nomarBgView.layer.insertSublayer(normalLayer, at:0)
nomarBgView.backgroundColor = .clear
cell.backgroundView = nomarBgView
let selectBgView = UIView(frame: bounds)
selectLayer.fillColor = UIColor(213.0, 230.0, 244.0).cgColor
selectBgView.layer.insertSublayer(selectLayer, at:0)
cell.selectedBackgroundView = selectBgView
}
UITableView cell加圆角
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- 自定义UITableViewCell 上面的控件设置圆角 自定义cell的drawRect 方法,并在该方法里面设...
- 实现UITableView的cell之间的圆角&间隙 废话不多说,直接上代码 第一步 去除系统默认tablevie...
- 开发中有遇到要把长方形的cell改为圆角的,网上查了资料发现好多网友在如下方法中重绘cell。具体代码大家可以自行...