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

Why isn't UITableViewCell's Separator View listed?

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

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

for view in cell.subviews {
    DDLogDebug(String(describing: type(of: view)))
}

这似乎发生在初步看来。如果我将 UITableView 单元格滚动到远离视图然后再次返回,UITableViewCell 的分隔符神奇地出现在子视图日志打印输出中。这是怎么回事?

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

您可以使用情节提要和编程方式删除分隔符。

Storyboard

只需单击 table view 并从 select separator 转到 attribute inspector 并将其从 default 更改为 none

Code

viewDidLoad

中使用下面的代码
customtableview.separatorStyle = .none

EDITED HIDE OR REMOVE SEPARATION FROM A SINGLE CELL

有人已经问过这个问题community.So here是处理单细胞分离的解决方案。

使用 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()
        }

    }
}

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

我在 iPhone X 上遇到了与 UITableView 分隔符相同的问题。我能够通过更改单元格背景解决它:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
   let cell: UITableViewCell = ...
   ...

   **cell.backgroundColor = .clear** // <= MAGIC LINE
   return cell
}