编辑时重置 UITableViewCell 的角半径

Corner Radius of UITableViewCell Being Reset when Editing

我有一个 UITableView,其中包含一个带有自定义角半径的 UITableViewCells 列表。滑动编辑时,e.i。 delete/insert,转角半径重置为0。

我尝试在编辑时设置圆角半径:

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

我也试过以类似的方式实现 willBeginEditingRowAt 来尝试让角半径保持不变。我尝试的最后一件事是在 trailingSwipeActionsConfigurationForRowAt 中创建自定义 UIContextualAction,但它没有改变任何东西。

大约四年前有一个similar question asked,但一直没有找到最终的解决方案。感谢任何帮助或见解!

您需要将圆角半径和边框设置为 contentView

cell.contentView.layer.cornerRadius = 13

将此代码添加到 UITableViewCell class 的 awakeFromNiblayoutSubviews 方法中。

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
}

希望对您有所帮助。