Swift - 設置tableView每一個分區cell圓角

 

1.// 從新繪製cell邊框spa

    func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {對象

            let cornerRadius: CGFloat = 10            it

            cell.backgroundColor = UIColor.clearColor()io

            let layer = CAShapeLayer()table

            let pathRef = CGPathCreateMutable()    class

            let bounds = CGRectInset(cell.bounds, 10, 0)test

            var addLine = false渲染

            if indexPath.row == 0 && indexPath.row == tableView.numberOfRowsInSection(indexPath.section)-1 {select

                CGPathAddRoundedRect(pathRef, nil, bounds, cornerRadius, cornerRadius)方法

            } else if indexPath.row == 0 {

                CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds))

                CGPathAddArcToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds), CGRectGetMidX(bounds), CGRectGetMinY(bounds), cornerRadius)

                CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds), CGRectGetMaxX(bounds), CGRectGetMidY(bounds), cornerRadius)

                CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds))

                addLine = true

            } else if indexPath.row == tableView.numberOfRowsInSection(indexPath.section)-1 {

                CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds))

                CGPathAddArcToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds), CGRectGetMidX(bounds), CGRectGetMaxY(bounds), cornerRadius)

                CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds), CGRectGetMaxX(bounds), CGRectGetMidY(bounds), cornerRadius)

                CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds))

            } else {

                CGPathAddRect(pathRef, nil, bounds)

                addLine = true

            }

            layer.path = pathRef

            //顏色修改

            layer.fillColor = UIColor.init(white: 1, alpha: 0.5).CGColor

            layer.strokeColor = UIColor.lightGrayColor().CGColor

            if addLine == true {

                let lineLayer = CALayer()

                let lineHeight = (1 / UIScreen.mainScreen().scale)

                lineLayer.frame = CGRectMake(CGRectGetMinX(bounds)+10, bounds.size.height-lineHeight, bounds.size.width-10, lineHeight)

                lineLayer.backgroundColor = tableView.separatorColor?.CGColor

                layer.addSublayer(lineLayer)

            }

            let testView = UIView(frame: bounds)

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

            testView.backgroundColor = UIColor.clearColor()

            cell.backgroundView = testView

    }

 

 

2.// 設置tableView每一個分區cell圓角

    func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {

        // 圓角弧度半徑

        let cornerRadius: CGFloat = 6

        // 設置cell的背景色爲透明,若是不設置這個的話,則原來的背景色不會被覆蓋

        cell.backgroundColor = UIColor.clearColor()

        // 建立一個shapeLayer

        let layer = CAShapeLayer()

        let backgroundLayer = CAShapeLayer() //顯示選中

        // 建立一個可變的圖像Path句柄,該路徑用於保存繪圖信息

        let pathRef = CGPathCreateMutable()

        // 獲取cellsize

        // 第一個參數,是整個 cell bounds, 第二個參數是距左右兩端的距離,第三個參數是距上下兩端的距離

        let bounds = CGRectInset(cell.bounds, 10, 0)

        

        // CGRectGetMinY:返回對象頂點座標

        // CGRectGetMaxY:返回對象底點座標

        // CGRectGetMinX:返回對象左邊緣座標

        // CGRectGetMaxX:返回對象右邊緣座標

        // CGRectGetMidX: 返回對象中心點的X座標

        // CGRectGetMidY: 返回對象中心點的Y座標

        

        // 這裏要判斷分組列表中的第一行,每組section的第一行,每組section的中間行

        // CGPathAddRoundedRect(pathRef, nil, bounds, cornerRadius, cornerRadius)

        if indexPath.row == 0 {

            // 初始起點爲cell的左下角座標

            CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds))

            // 起始座標爲左下角,設爲p,(CGRectGetMinX(bounds), CGRectGetMinY(bounds))爲左上角的點,設爲p1(x1,y1)(CGRectGetMidX(bounds), CGRectGetMinY(bounds))爲頂部中點的點,設爲p2(x2,y2)。而後鏈接p1p2爲一條直線l1,鏈接初始點pp1成一條直線l,則在兩條直線相交處繪製弧度爲r的圓角。

            CGPathAddArcToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds), CGRectGetMidX(bounds), CGRectGetMinY(bounds), cornerRadius)

            CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds), CGRectGetMaxX(bounds), CGRectGetMidY(bounds), cornerRadius)

            // 終點座標爲右下角座標點,把繪圖信息都放到路徑中去,根據這些路徑就構成了一塊區域了

            CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds))

        } else if indexPath.row == tableView.numberOfRowsInSection(indexPath.section)-1 {

            // 初始起點爲cell的左上角座標

            CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds))

            CGPathAddArcToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds), CGRectGetMidX(bounds), CGRectGetMaxY(bounds), cornerRadius)

            CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds), CGRectGetMaxX(bounds), CGRectGetMidY(bounds), cornerRadius)

            // 添加一條直線,終點座標爲右下角座標點並放到路徑中去

            CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds))

        } else {

            // 添加cellrectangle信息到path中(不包括圓角)

            //假如用填充色,用這個

            //        CGPathAddRect(pathRef, nil, bounds)

            //假如只要邊框

            CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds))

            CGPathAddLineToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds))

            CGPathMoveToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds))

            CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds))

        }

        // 把已經繪製好的可變圖像路徑賦值給圖層,而後圖層根據這圖像path進行圖像渲染render

        layer.path = pathRef

        backgroundLayer.path = pathRef

        // 按照shape layerpath填充顏色,相似於渲染render

        // layer.fillColor = [UIColor colorWithWhite:1.f alpha:0.8f].CGColor

        layer.strokeColor = UIColor.blackColor().CGColor

        layer.fillColor = UIColor.clearColor().CGColor

        

        // view大小與cell一致

        let roundView = UIView(frame: self.view.bounds)

        // 添加自定義圓角後的圖層到roundView

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

        roundView.backgroundColor = UIColor.clearColor()

        // cell的背景view

        cell.backgroundView = roundView

        

        // 以上方法存在缺陷當點擊cell時仍是出現cell方形效果,所以還須要添加如下方法

        // 若是你 cell 已經取消選中狀態的話,那如下方法是不須要的.

        let selectedBackgroundView = UIView(frame: self.view.bounds)

        backgroundLayer.fillColor = UIColor.cyanColor().CGColor

        selectedBackgroundView.layer.insertSublayer(backgroundLayer, atIndex: 0)

        selectedBackgroundView.backgroundColor = UIColor.clearColor()

        cell.selectedBackgroundView = selectedBackgroundView

       // 在使用上面代碼前須要把tableView默認的分割線設置爲None

    }

相關文章
相關標籤/搜索