set tableview cell corner radius swift 2.0

时间:2015-12-24 15:30:53

标签: ios swift uitableview

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

enter image description here

3 个答案:

答案 0 :(得分:12)

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的建议......还有另一个具有相同名称的属性内容角..所以检查一下)

答案 1 :(得分:11)

您可以在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

以下是我的代码的输出

enter image description here

答案 2 :(得分:3)

您的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

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