分组 UITableView 中第一个和最后一个单元格的自定义圆角

Cla*_*aus 2 iphone rounded-corners cornerradius uitableview

我的 iPhone 应用程序当前显示一个包含 2 个部分的分组表。我尝试使用以下方法更改每个部分的第一个和最后一个单元格(或在只有一行的部分的情况下在同一单元格上)的圆角半径:

cell.layer.cornerRadius = 4;
Run Code Online (Sandbox Code Playgroud)

或作用于整个 tableview:

self.tableView.layer.cornerRadius = 4;
Run Code Online (Sandbox Code Playgroud)

但没有运气......当然,我在执行此操作之前已经导入了QuartzCore。看起来只有当表格以普通样式显示时才可以自定义角。

理想的解决方案是自定义第一个角的顶角和最后一个角的底角。

有什么建议?

mea*_*ers 8

以下是 Swift 4.2 中的工作原理:

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath)
{
    let cornerRadius = 5
    var corners: UIRectCorner = []

    if indexPath.row == 0
    {
        corners.update(with: .topLeft)
        corners.update(with: .topRight)
    }

    if indexPath.row == tableView.numberOfRows(inSection: indexPath.section) - 1
    {
        corners.update(with: .bottomLeft)
        corners.update(with: .bottomRight)
    }

    let maskLayer = CAShapeLayer()
    maskLayer.path = UIBezierPath(roundedRect: cell.bounds,
                                  byRoundingCorners: corners,
                                  cornerRadii: CGSize(width: cornerRadius, height: cornerRadius)).cgPath
    cell.layer.mask = maskLayer
}
Run Code Online (Sandbox Code Playgroud)


pro*_*dor 3

最简单的方法是:

  1. 在 xib 文件中创建自定义单元格,设置其唯一标识符,例如:@"beginCell", @"middleCell", @"endCell"。您可以根据本教程进行制作:http://www.bdunagan.com/2009/06/28/custom-uitableviewcell-from-a-xib-in-interface-builder/

  2. 在方法中, - (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 您必须找到您位于该部分的中间还是开始/结尾,并使用具有正确标识符的单元格。