iOS

Swift3.0 tableView 圆角cell

2018-11-22  本文已影响0人  墨林灵

functableView(_tableView:UITableView, willDisplay cell:UITableViewCell, forRowAt indexPath:IndexPath) {

        letsection = indexPath.section

        ifsection !=0{

            return

        }

        letcornerRadius:CGFloat=4.0

        cell.backgroundColor = UIColor.clear

        letlayer =CAShapeLayer()

        letbackgroundLayer =CAShapeLayer()

        letpathRef =CGMutablePath()

        letbounds = cell.bounds

        letnumberOfRows =self.tableView.numberOfRows(inSection: indexPath.section)

        varneedSeparator =false

        if(indexPath.row==0&& numberOfRows ==1) {

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

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

            needSeparator =true;

        }elseifindexPath.row== (numberOfRows-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)

            needSeparator =true

        }

        layer.path= pathRef

        backgroundLayer.path= pathRef

        layer.fillColor = UIColor.white.cgColor

        if(needSeparator) {

            letlineLayer =CALayer()

            letlineHeight = (1.0/UIScreen.main.scale)

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

            lineLayer.backgroundColor = self.tableView.separatorColor?.cgColor

            layer.addSublayer(lineLayer)

        }

        letroundView =UIView(frame: bounds)

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

        roundView.backgroundColor = UIColor.clear

        cell.backgroundView= roundView

    }

}

上一篇下一篇

猜你喜欢

热点阅读