set tableview cell corner radius swift 2.0

Joh*_*ohn 6 uitableview ios swift

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

在此输入图像描述

Sai*_*nam 14

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
    }
}
}
Run Code Online (Sandbox Code Playgroud)

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


Pal*_*mbh 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
Run Code Online (Sandbox Code Playgroud)

下面是我的代码的输出

在此输入图像描述


Ras*_*n L 3

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

cell.contentView.layer.cornerRadius = 10
cell.contentView.layer.masksToBounds = true
Run Code Online (Sandbox Code Playgroud)

要自定义边框,您可以

cell.layer.borderColor = UIColor.grayColor().CGColor
cell.layer.borderWidth = 5
Run Code Online (Sandbox Code Playgroud)

更新

要将其添加到您的viewForHeaderInSection 请让 view = UIView() 并将半径添加到视图中

view.layer.cornerRadius = 10
view.layer.masksToBounds = true
Run Code Online (Sandbox Code Playgroud)

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

  • “但不工作”根本没有任何帮助。您需要详细描述您尝试过的内容,以及它如何不满足您的需求,*也详细*。 (3认同)