【问题标题】:set tableview cell corner radius swift 2.0设置tableview单元角半径swift 2.0
【发布时间】:2016-03-31 00:35:20
【问题描述】:

我有一个小问题,我想像下图那样更改单元角半径

【问题讨论】:

  • 我想像上图第 1 部分那样设置角
  • 我认为这在 iOS 上是不可能的

标签: ios swift uitableview


【解决方案1】:

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

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

【讨论】:

  • 它设置了整个单元格角半径我想设置部分明智
  • 如果你还没有使用它,你需要将 viewForHeaderInSection 添加到你的 tableView 中。检查更新的代码。
  • 将您的代码添加到您的问题中,这样更容易为您提供帮助。
  • “但不工作”没有根本没有帮助。您需要详细描述您尝试了什么,以及它如何不符合您的需求,还要详细。
【解决方案2】:

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

下面是我的代码输出

【讨论】:

  • 如果这不起作用,请尝试cell!.contentView.layer。我怀疑是单元格的内容视图具有边框和圆角,而不是单元格。
  • @DuncanC 请查看我编辑的答案。这段代码对我有用。
  • 看起来 OP 想要一个分段的表格视图,其中各个部分在整个部分周围用圆角矩形分组。这更复杂。
  • 我想对 tableview 单元格的部分进行半径处理,而不是 tableview 单元格的行见上图
  • 我不知道为什么这个答案比我的投票多
【解决方案3】:
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 角类型(输入时来自 xcode 的建议......还有另一个同名的属性内容角......所以检查一下)

【讨论】:

  • 这应该被标记为正确答案,至少它对我有帮助:-)
  • @SaiPavanParanam 非常感谢,在搜索了几天后我失去了希望,最后你的解决方案对我的场景进行了轻微修改。
  • 它根本不工作,请检查您的视图控制器。
猜你喜欢
  • 2016-06-14
  • 2021-03-02
  • 2016-07-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多