Ios 将tableview单元格角半径设置为swift 2.0
我有个小问题,我想改变单元格的角半径,如下图所示Ios 将tableview单元格角半径设置为swift 2.0,ios,swift,uitableview,Ios,Swift,Uitableview,我有个小问题,我想改变单元格的角半径,如下图所示 您的tableView似乎包含一个UIView,所以只需将这些行添加到cellForRowAtIndexPath中即可。如果没有,请添加UIView并将半径添加到UIView,然后将该视图添加到单元格中(cell.addSubView(YOURVIEW)) 要自定义边框,您可以 cell.layer.borderColor = UIColor.grayColor().CGColor cell.layer.borderWidth = 5 更新 要
您的tableView似乎包含一个
UIView
,所以只需将这些行添加到cellForRowAtIndexPath
中即可。如果没有,请添加UIView并将半径添加到UIView,然后将该视图添加到单元格中(cell.addSubView(YOURVIEW)
)
要自定义边框,您可以
cell.layer.borderColor = UIColor.grayColor().CGColor
cell.layer.borderWidth = 5
更新
要将此添加到您的视图ForHeaderin部分
创建视图let view=UIView(),并将半径添加到视图中
view.layer.cornerRadius = 10
view.layer.masksToBounds = true
并添加所需的其他属性并返回该视图。您可以在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
下面是我的代码的输出
实际上我们要做的是,若节只有一行,那个么我们在所有的边上都做,若节有多行,那个么我们在第一行的顶部做,在最后一行的底部做。。。属性BottomLeft、BottomRight、topLeft、TopRight应为rect corner类型(键入时来自xcode的建议…还有另一个同名的属性内容角..因此请检查)我想像上面的图像部分1I那样设置角点我不认为这在IOS上是可能的如果不起作用,请尝试
cell!。contentView.layer
。我怀疑是单元格的内容视图有边框和圆角,而不是单元格。@Duncac请查看我编辑的答案。这段代码是为我编写的。看起来OP需要一个分区表视图,其中分区用一个圆角矩形围绕整个分区进行分组。更复杂的是,我想知道tableview单元格的半径部分,而不是tableview单元格的行,看上图我不知道为什么这个答案比我的答案有更多的投票权它设置了整个单元格的角半径我想明智地设置部分,那么如果你还没有使用它,你需要在你的tableview中添加ViewForHeaderSection。检查更新的代码。我也会尝试但不工作将代码添加到问题中,这样会更容易帮助您。“但不工作”一点帮助都没有。你需要详细描述你尝试了什么,以及它如何不满足你的需求。这应该被标记为正确的答案,至少它帮助了我:-)@saipvanparanam非常感谢你,在搜索了几天之后,我失去了希望,最终你的解决方案对我的场景进行了轻微的修改。它根本不起作用。请检查你的视图控制器。
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
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
}
}
}