swift UITableViewCell 绘制边框加圆角

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {

    let cornerRadius: CGFloat = 10

    cell.backgroundColor = UIColor.clear

    let layer = CAShapeLayer()

    let pathRef = CGMutablePath()

    let bounds = cell.bounds.insetBy(dx: 10, dy: 0)

    if indexPath.row == 0 && indexPath.row == tableView.numberOfRows(inSection: indexPath.section)-1 {

       pathRef.__addRoundedRect(transform: nil, rect: bounds, cornerWidth: cornerRadius, cornerHeight: cornerRadius)

    } else if indexPath.row == 0 {

       pathRef.move(to: CGPoint(x: bounds.minX, y: bounds.maxY))
       
       pathRef.addArc(tangent1End: CGPoint(x: bounds.minX, y: bounds.minY), tangent2End: CGPoint(x: bounds.midX, y: bounds.minY), radius: cornerRadius)
       
       pathRef.addArc(tangent1End: CGPoint(x: bounds.maxX, y: bounds.minY), tangent2End: CGPoint(x: bounds.maxX, y: bounds.midY), radius: cornerRadius)
       
       pathRef.addLine(to: CGPoint(x: bounds.maxX, y: bounds.maxY))
       
    } else if indexPath.row == tableView.numberOfRows(inSection: indexPath.section)-1 {

       pathRef.move(to: CGPoint(x: bounds.minX, y: bounds.minY))

       pathRef.addArc(tangent1End: CGPoint(x: bounds.minX, y: bounds.maxY), tangent2End: CGPoint(x: bounds.midX, y: bounds.maxY), radius: cornerRadius)
       
       pathRef.addArc(tangent1End: CGPoint(x: bounds.maxX, y: bounds.maxY), tangent2End: CGPoint(x: bounds.maxX, y: bounds.midY), radius: cornerRadius)
       
       pathRef.addLine(to: CGPoint(x: bounds.maxX, y: bounds.minY))
       
    } else {
        
       pathRef.addRect(bounds);
    }

    layer.path = pathRef

    //颜色修改

    layer.fillColor = UIColor.white.cgColor

//需要设置边框线加上这一行
// layer.strokeColor = UIColor.red.cgColor

    layer.lineWidth = 0.3

    let testView = UIView(frame: bounds)

    testView.layer.insertSublayer(layer, at: 0)

    cell.backgroundView = testView
}

lazy var tabelView:UITableView = {
    let tableView = UITableView(frame:view.bounds,style: UITableView.Style.grouped)
    tableView.delegate = self
    tableView.dataSource = self
    tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell1")
    tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell2")
    tableView.separatorStyle = .none
    tableView.estimatedRowHeight = 0.0;
    tableView.estimatedSectionHeaderHeight = 0.0;
    tableView.estimatedSectionFooterHeight = 0.0;
    return tableView
}()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值