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
}()
上一篇:layer弹出框基本使用


下一篇:js中最最常用的函数封装,方便调用