编辑时UITableViewCell的圆角半径被重置

4

我有一个UITableView,其中包含一系列具有自定义圆角半径的UITableViewCells。当滑动以进行编辑,例如删除/插入时,圆角半径将重置为0。

Image of cell's corner radius being reset

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

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,但它没有改变任何内容。

大约四年前就有一个类似的问题,但从未找到确定性解决方案。感谢任何帮助或见解!


1
为什么不在“layoutSubviews”函数中使用“cornerRadius”来设置“UITableViewCell”的圆角? - Quang Dam
1个回答

8
您需要将圆角半径和边框设置为contentView
cell.contentView.layer.cornerRadius = 13

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

我希望这能帮助到你。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接