【发布时间】:2020-07-14 21:11:44
【问题描述】:
在我的表格视图中,我的tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) 中有逻辑来确定单元格的顶部、底部是否有圆角,或者根本没有圆角。
override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
if indexPath.row == 0 {
// round top 2 corners
let maskLayer = CAShapeLayer()
maskLayer.path = UIBezierPath(roundedRect: cell.bounds, byRoundingCorners: [UIRectCorner .TopLeft, UIRectCorner .TopRight], cornerRadii: CGSizeMake(4, 4)).CGPath
cell.layer.mask = maskLayer
}
if indexPath.section != 1 {
if indexPath.row == self.tableView.numberOfRowsInSection(indexPath.section) - 1 {
let maskLayer = CAShapeLayer()
maskLayer.path = UIBezierPath(roundedRect: cell.bounds, byRoundingCorners: [UIRectCorner .BottomLeft, UIRectCorner .BottomRight], cornerRadii: CGSizeMake(4, 4)).CGPath
cell.layer.mask = maskLayer
}
}
}
这可行,但是在我对 tableview 实施编辑后,删除按钮发生了奇怪的事情。
带圆角的代码:
当我去掉圆角时:
这真的让我很困惑,因为我不确定自己做错了什么。有人可以帮忙吗?
【问题讨论】:
-
你试过加
cell.layer.maskToBounds = true吗? -
您可能不应该直接对单元格的视图执行此操作。尝试将图层蒙版应用到单元格的
contentView上,看看是否可行。
标签: ios swift uitableview rounded-corners