2017-03-06 160 views
1

我要让我的tableView看起来像这样的:四舍五入的UITableViewCell

enter image description here

,我有问题的。只有当我点击牢房时,我的右角才圆了。当视图显示它看起来像这样:

enter image description here

和这样的自来水后:

enter image description here

这里是我的代码:

extension UITableViewCell { 
     func round(corners: UIRectCorner, withRadius radius: CGFloat) { 
     let mask = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius)) 
     let shape = CAShapeLayer() 
     shape.frame = self.bounds 
     shape.path = mask.cgPath 
     self.layer.mask = shape 
     } 
    } 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    ... 
    if numberOfRows(in: indexPath.section) == 1 { 
     cell.round(corners: [.bottomLeft, .bottomRight, .topLeft, .topRight], withRadius: 8) 
    } else { 
     if indexPath.row == 0 { 
     cell.round(corners: [.topLeft, .topRight], withRadius: 8) 
     } 
     if indexPath.row == numberOfRows(in: indexPath.section) - 1 { 
     cell.round(corners: [.bottomLeft, .bottomRight], withRadius: 8) 
     } 
    } 
    return cell 
    } 
+0

尝试将这些代码移动到'willDisplayCell' – Tj3n

回答

3

这是因为当你第一次将你的细胞四舍五入时,它不会在任何超视图上绘制,因此它的框架是未知的。试图把在角落

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { 

    } 

另外,我强烈建议你不要四舍五入的细胞本身,而是添加背景着眼于细胞,使细胞的背景颜色清晰(单元格的内容视图的为好),使该视图的圆角。祝你好运!

+1

非常感谢,我会尝试背景视图的事情,也许它会解决一些其他问题。 – dsk1306

0

你必须创建自定义的UITableViewCell为您的设计。并为tableviewcell Parentview采取cornerradius = 2。

+0

我已经有自定义单元格,但它非常简单(只有2个iboutlets)。而且我不确定我是否理解了答案的第二部分。 – dsk1306

+0

parentview.cornerradius = 2(自定义单元格) –

0

根据你的屏幕截图, 我认为,你的约束没有正确设置。

您可以使用自定义xib作为单元格。