set tableview cell corner radius swift 2.0

我有小问题,我想改变像下图像的单元角半径

在此处输入图像描述

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) { if (tableView == self.orderDetailsTableView) { //Top Left Right Corners let maskPathTop = UIBezierPath(roundedRect: cell.bounds, byRoundingCorners: [.TopLeft, .TopRight], cornerRadii: CGSize(width: 5.0, height: 5.0)) let shapeLayerTop = CAShapeLayer() shapeLayerTop.frame = cell.bounds shapeLayerTop.path = maskPathTop.CGPath //Bottom Left Right Corners let maskPathBottom = UIBezierPath(roundedRect: cell.bounds, byRoundingCorners: [.BottomLeft, .BottomRight], cornerRadii: CGSize(width: 5.0, height: 5.0)) let shapeLayerBottom = CAShapeLayer() shapeLayerBottom.frame = cell.bounds shapeLayerBottom.path = maskPathBottom.CGPath //All Corners let maskPathAll = UIBezierPath(roundedRect: cell.bounds, byRoundingCorners: [.TopLeft, .TopRight, .BottomRight, .BottomLeft], cornerRadii: CGSize(width: 5.0, height: 5.0)) let shapeLayerAll = CAShapeLayer() shapeLayerAll.frame = cell.bounds shapeLayerAll.path = maskPathAll.CGPath if (indexPath.row == 0 && indexPath.row == tableView.numberOfRowsInSection(indexPath.section)-1) { cell.layer.mask = shapeLayerAll } else if (indexPath.row == 0) { cell.layer.mask = shapeLayerTop } else if (indexPath.row == tableView.numberOfRowsInSection(indexPath.section)-1) { cell.layer.mask = shapeLayerBottom } } } 

实际上我们正在做的是如果section只有一行然后我们在所有方面都这样做,如果section有多行,那么我们在第一行顶部和最后一行底部执行…属性BottomLeft,BottomRight,topLeft,TopRight应该是rect corner类型(当您输入时来自xcode的建议…还有另一个具有相同名称的属性内容角…所以检查一下)

您可以在swift 2.0中使用以下代码

将此代码放在cellForRowAtIndexpath方法中:

 cell!.layer.cornerRadius=10 //set corner radius here cell!.layer.borderColor = UIColor.blackColor().CGColor // set cell border color here cell!.layer.borderWidth = 2 // set border width here 

下面是我的代码的输出

在此处输入图像描述

你的tableView似乎包含一个UIView所以只需在cellForRowAtIndexPath添加这些行。 如果没有添加UIView并将半径添加到UIView,只需将该视图添加到您的单元格( cell.addSubView(YOURVIEW) )。

 cell.contentView.layer.cornerRadius = 10 cell.contentView.layer.masksToBounds = true 

要自定义边框

 cell.layer.borderColor = UIColor.grayColor().CGColor cell.layer.borderWidth = 5 

更新

要在viewForHeaderInSection添加它,请创建一个视图,让view = UIView()并将半径添加到视图中

 view.layer.cornerRadius = 10 view.layer.masksToBounds = true 

并添加您需要的其他属性并返回该视图。