Swift3.0 tableView 圆角cell

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

    }

}

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容