Delete button not showing on UITableViewCell with

2019-05-31 18:04发布

问题:

In my tableview, I have logic in my tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) to determine if a cell has rounded corners on top, bottom, or no rounded corners at all.

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
            }
        }

    }

This works, but after I have implemented editing into the tableview, strange things happen to the delete button.

With the rounded corners code:

When I remove the rounded corners:

This really confuses me because I'm not sure what I'm doing wrong. Can anyone help?

回答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
            }
        }

    }

use cell.contentView.layer.mask = maskLayer