dsk*_*306 3 uitableview ios swift
我想让我的 tableView 看起来像这样:
我有问题。仅当我点击单元格后,我的右角才会变圆。当视图出现时,它看起来像这样:
点击后像这样:
这是我的代码:
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
}
Run Code Online (Sandbox Code Playgroud)
这是因为当您第一次围绕单元格时,它没有在任何超级视图上绘制,因此它的框架是未知的。尝试将角变圆
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
}
Run Code Online (Sandbox Code Playgroud)
另外,我强烈建议您不要将单元格本身圆化,而是向单元格添加背景视图,使单元格的背景颜色清晰(单元格的内容视图也是如此),并为该视图设置圆角。祝你好运!