删除自定义 UITableViewCell 框架问题的行

Delete Rows of custom UITableViewCell frame issue

我对 Xcode 和 Swift 编程比较陌生,在我的第一个应用程序中,它包含一个带有自定义 TableViewCell 的表格视图,它覆盖可变框架以不覆盖整个宽度观点:

override var frame: CGRect {
        get {
            return super.frame
        }

        set {
            var frame = newValue
            frame.origin.x += 175
            frame.size.width -= 350
            super.frame = frame
        }
    }

一切都按预期工作,但是当我使用 self.tableView.deleteRows(at: [path], with: .automatic) 删除 Tableview 单元格时,该单元格出现了一个奇怪的问题;看起来单元格再次获得比旧框架新的(更小的)框架,但只有可见单元格;如果重新加载单元格,问题就消失了。所以它只是在删除之后发生;如果单元格离开屏幕然后再次出现,它会恢复到正常大小。 我的猜测是 `newValue 已经是一个自定义框架,然后再次进行自定义,这使得它变得更小;但我不知道如何解决这个问题!

以下是 iPad 版本的屏幕截图:

Normal tableview

After deleting

如有任何帮助,我们将不胜感激!如果您需要更多信息,请在评论中告诉我!谢谢!

我建议你不要像你那样干扰细胞的框架

  1. 将单元格的 backgroundColor 设置为 UIColor.clear
  2. 在单元格 contentView 上创建自定义视图,水平居中且比单元格本身窄 350 点。

或者

  1. 使整个 tableView 的 width 比屏幕宽度少 350 点
  2. 忘记更改单元格的 width

对于第一个建议,视图层次结构和自动布局约束看起来类似于:

The problem is with a custom view I will run into even more troubles because the height of each cell is different from one another

这完全是另一个话题。您应该使用动态单元格高度,在 RayWenderlich tutorial

中有详细描述

基本上,您需要正确设置自动布局约束并在 viewDidLoad:

中执行此操作
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 140

气馁的做法:

My guess is that `newValueis already a custom frame and then gets customized again which makes it even smaller; but I have no idea how to adress this issue!

如果您仍想使用您的方法,不要只减少值,使用UIScreentableView从头开始计算的 width

而不是这个

frame.origin.x += 175
frame.size.width -= 350

这样做:

frame.origin.x = 175
frame.size.width = UIScreen.main.bounds.size.width - 350