【问题标题】:Delete button not showing on UITableViewCell with rounded corners删除按钮未显示在带有圆角的 UITableViewCell 上
【发布时间】: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


【解决方案1】:
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.contentView.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.contentView.layer.mask = maskLayer
            }
        }

    }

使用 cell.contentView.layer.mask = maskLayer

【讨论】:

  • 嗯...它显示了删除按钮,但单元格似乎没有被四舍五入。我尝试将单元格背景颜色设置为清除,并将 contentView 背景颜色设置为白色,这可行,但红色圆圈周围的区域显示了 tableview 背景。
【解决方案2】:

简单的方法是在 uitableviewcell 上添加一个带圆角的全尺寸视图。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2011-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多