UiTableViewCell 最顶部最底部添加圆角 支持多分组

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

        

        if (cell.responds(to: #selector(getter: UIView.tintColor))){

            if tableView == self.tableView {

                let cornerRadius: CGFloat = 10

                cell.backgroundColor = .clear

                let layer: CAShapeLayer = CAShapeLayer()

                let path: CGMutablePath = CGMutablePath()

                

                cell.updateConstraints()

                let bounds: CGRect = cell.bounds

           // bounds.insetBy(dx: 25.0, dy: 0.0)

               

                var addLine: Bool = false

                

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

                    path.addRoundedRect(in: bounds, cornerWidth: cornerRadius, cornerHeight: cornerRadius)

                    

                } else if indexPath.row == 0 {

                    path.move(to: CGPoint(x: bounds.minX, y: bounds.maxY))

                    path.addArc(tangent1End: CGPoint(x: bounds.minX, y: bounds.minY), tangent2End: CGPoint(x: bounds.midX, y: bounds.minY), radius: cornerRadius)

                    path.addArc(tangent1End: CGPoint(x: bounds.maxX, y: bounds.minY), tangent2End: CGPoint(x: bounds.maxX, y: bounds.midY), radius: cornerRadius)

                    path.addLine(to: CGPoint(x: bounds.maxX, y: bounds.maxY))

                    

                } else if indexPath.row == (tableView.numberOfRows(inSection: indexPath.section) - 1) {

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

                    path.addArc(tangent1End: CGPoint(x: bounds.minX, y: bounds.maxY), tangent2End: CGPoint(x: bounds.midX, y: bounds.maxY), radius: cornerRadius)

                    path.addArc(tangent1End: CGPoint(x: bounds.maxX, y: bounds.maxY), tangent2End: CGPoint(x: bounds.maxX, y: bounds.midY), radius: cornerRadius)

                    path.addLine(to: CGPoint(x: bounds.maxX, y: bounds.minY))

                    

                } else {

                    path.addRect(bounds)

                    addLine = true

                }

                

                layer.path = path

                layer.fillColor = UIColor.white.cgColor

                

                if addLine {

                    let lineLayer: CALayer = CALayer()

                    let lineHeight: CGFloat = 1.0 / UIScreen.main.scale

                    lineLayer.frame = CGRect(x: bounds.minX + 10.0, y: bounds.size.height - lineHeight, width: bounds.size.width, height: lineHeight)

                    lineLayer.backgroundColor = tableView.separatorColor?.cgColor

                    layer.addSublayer(lineLayer)

                }

                

                let testView: UIView = UIView(frame: bounds)

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

                testView.backgroundColor = .clear

                cell.backgroundView = testView

            }

        }

    }

如果要加边距需要重写cell 

override var frame: CGRect {

        get {

            return super.frame

        }

        set {

            var frame = newValue

            frame.origin.x += 15

            frame.size.width -= 2 * 15

            super.frame = frame

        }

    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值