首页 > 解决方案 > 编辑时 UITableViewCell 的角半径被重置

问题描述

我有一个 UITableView,其中包含一个具有自定义角半径的 UITableViewCells 列表。滑动编辑、删除/插入时,圆角半径重置为 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,但它不会改变任何内容。

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

标签: iosswiftxcodeuitableviewcornerradius

解决方案


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

cell.contentView.layer.cornerRadius = 13

将此代码添加到类的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
}

我希望这能帮到您。


推荐阅读