为什么没有列出 UITableViewCell 的 Separator View?

Seb*_*nik 0 uitableview separator ios subviews swift

我想删除某些 UITableViewCell separators(不是全部)。

我在 UI 中看到它们,但不明白为什么当我打印出子视图层次结构时它们没有被列出?

for view in cell.subviews {
    DDLogDebug(String(describing: type(of: view)))
}
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

这似乎发生在初始视图中。如果我将 UITableView 单元格滚动到远处然后再返回,UITableViewCell 的分隔符会神奇地出现在子视图日志打印输出中。这里发生了什么?

注意: 我只在模拟器上测试过,因为我目前没有 iPhone X。

Seb*_*nik 5

在单元格外使用 UITableViewCell似乎并不像简单地对单元格进行子类化并从其内部进行控制那样可靠。

因此我的解决方案如下:

class myCell: UITableViewCell {

    var withSeparator: Bool = true

    func layoutSubviews() {
        super.layoutSubviews()

        if !withSeparator {
            self.removeBottomSeparator()
        }

    }
...



extension UITableViewCell {
    func removeBottomSeparator() {
        // Remove it if it's found.
        for view in self.subviews where String(describing: type(of: view)).hasSuffix("SeparatorView") {
            view.removeFromSuperview()
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

使用这种方法可以让我SeparatorView每次都能找到并删除它。