表格视图单元格的圆角半径

7
我需要像下图中那样更改单元格的圆角半径: enter image description here

我想设置角落,就像上面的图像部分1一样。 - John
我认为在iOS上这是不可能的。 - Fabio Berger
6个回答

17
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
    }
}
}

我们实际上正在做的是,如果部分只有一行,则在所有边上进行操作;如果部分有多行,则在第一行顶部和最后一行底部进行操作...属性BottomLeft、BottomRight、topLeft、TopRight应该是rect corner类型(在输入时来自xcode的建议...还有一个具有相同名称的content corner属性..因此请检查)


这应该被标记为正确答案,至少对我有帮助 :-) - Hernan Arber
@SaiPavanParanam非常感谢,经过数天的搜索我已经失去了希望,最终您的解决方案对我的情况进行了轻微修改并奏效。 - Arshad Shaik
完全不起作用,请检查您的视图控制器。 - Akash Raghani
我的应用程序从未进入willDisplayCell方法。可能是什么问题? - Mert Köksal

14

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

以下是我代码输出的结果

在此输入图片描述


@DuncanC 请查看我的编辑答案。这段代码对我有效。 - Pallavi Nikumbh
1
看起来OP想要一个分组的表视图,其中各个部分都用圆角矩形围绕整个部分。这更加复杂。 - Duncan C
1
我不知道为什么这个答案比我的得票数更高。 - SaiPavanParanam
@PallaviNikumbh 您的答案实际上没有解决他的问题。 - SaiPavanParanam
@PallaviNikumbh,您实际上是给整个单元格添加边框。他只需要按部分添加。 - SaiPavanParanam
显示剩余3条评论

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中添加此功能, 需要创建一个视图 let view = UIView() 并向您的视图添加半径。

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

并添加其他所需属性并返回该视图。

我想要按部分设置圆角,而不是整个单元格。 - John
如果您尚未使用它,则需要将viewForHeaderInSection添加到tableView中。请检查更新后的代码。 - Rashwan L
我也尝试了https://dev59.com/EGMk5IYBdhLWcg3w8iaC,但没有成功。 - John
请将您的代码添加到问题中,这样更容易帮助您。 - Rashwan L
3
“但不管用”这样的描述毫无帮助。你需要详细描述你尝试了什么,以及它如何不满足你的需求,同时也要详细说明。 - Duncan C

1

我设置了contentView.layer.cornerRadius = 10,但仍然没有显示;这是由于cell.contentView.backgroundColor默认为nil,导致背景色透明,因此它的cell.backgroundColor作为其背景颜色。

将单元格的背景颜色设置为.clear,在我的情况下会显示tableView的背景颜色(非白色),然后会按预期显示圆角,如下所示:

// Cell's init()
override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        backgroundColor = .clear // this will show parent views bkgColor 
                                 //(in my case non-white) 
                                 // where the sharp corner is

        contentView.layer.cornerRadius = 10.0 // set corner radius
        contentView.backgroundColor = .white // set tableViewCells background color to what you want

        /* do stuff */
}

0

尝试:

override func draw(_ rect: CGRect) {
        super.draw(rect)
        self.layer.cornerRadius = 8
        self.clipsToBounds = true
    }

在您的 TableViewCell 中

或者:

cell.layer.cornerRadius = 8
cell.clipsToBounds = true

0
最好的方法是在awakeFromNib中更改角半径,并确保将其clipToBounds设置为true。
self.layer.cornerRadius = 4.0
self.clipToBounds = true

或者您可以将一个视图放置在单元格内作为背景视图,并设置其圆角半径和clipToBounds属性。

self.backgroundView.layer.cornerRadius = 4.0
self.backgroundView.clipToBounds = true

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接