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
}()

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...