编辑时重置 UITableViewCell 的角半径

Jor*_* C. 3 xcode cornerradius uitableview ios swift

我有一个 UITableView,它有一个带有自定义圆角半径的 UITableViewCells 列表。滑动编辑时,ei 删除/插入,圆角半径重置为 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))
    }
}
Run Code Online (Sandbox Code Playgroud)

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

大约四年前有人问类似的问题,但一直没有找到最终的解决方案。任何帮助或见解表示赞赏!

Kom*_*ani 5

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

cell.contentView.layer.cornerRadius = 13
Run Code Online (Sandbox Code Playgroud)

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

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
}
Run Code Online (Sandbox Code Playgroud)

我希望这能帮到您。