iosswiftuitableviewcornerradius

Corner Radius of UITableViewCell Being Reset when Editing


I have a UITableView that has a list of UITableViewCells with custom corner radii. When swiping to edit, e.g. delete/insert, the corner radius is reset back to 0.

Image of cell's corner radius being reset

I've tried to set the corner radius while editing:

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
    guard let cell = tableView.cellForRow(at: indexPath) as? HabitTableViewCell else { return }
    cell.layer.cornerRadius = 13
    if editingStyle == .delete {
        controller.delete(habit: frc.object(at: indexPath))
    }
}

I've also tried implementing willBeginEditingRowAt in a similar way to try to get the corner radius to stay set. The last thing I tried was creating a custom UIContextualAction in trailingSwipeActionsConfigurationForRowAt, but it doesn't change anything.

There was a similar question asked about four years ago, but no conclusive solution was ever found. Any help or insight is appreciated!


Solution

  • You need to set corner radius and border to contentView.

    cell.contentView.layer.cornerRadius = 13
    

    Add this code to awakeFromNib or layoutSubviews method of UITableViewCell class.

    override func awakeFromNib() {
        super.awakeFromNib()
        self.contentView.layer.cornerRadius = 13
    
        // Your border code here (set border to contentView)
        self.contentView.layer.borderColor = UIColor.blue.cgColor
        self.contentView.layer.borderWidth = 3
    }