此分隔符在普通 View 中是不可见的。我将所有分隔层设置为红色并看到了它。如果我设置 tableView.separatorStyle = .none
,则此分隔符和普通分隔符不会添加到单元格中。易于重现:
添加 TableViewController
向单元格添加代码:
override func layoutSubviews() { super.layoutSubviews() let separators = self.subviews.filter({$0.bounds.height < 1 && $0.bounds.height > 0}) separators.forEach({ $0.layer.backgroundColor = UIColor.red.cgColor })
这是什么?
最佳答案
替换分隔符的正确方法,或以某种方式显示它或为每个部分显示一个不同的方法是这样的:
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
switch indexPath.section {
case 1:
cell.separatorInset = UIEdgeInsetsMake(0.0, tableView.bounds.size.width, 0.0, 0);
let additionalSeparatorThickness = CGFloat(1)
let additionalSeparator: UIView = UIView(frame: CGRect(x: 15.0, y: cell.frame.size.height - additionalSeparatorThickness,
width: cell.frame.size.width-30.0,
height: additionalSeparatorThickness))
additionalSeparator.backgroundColor = UIColor.Red
cell.addSubview(additionalSeparator)
default:
break;
}
}
在上面的示例中(对线宽、高度、ecc 进行了一些自定义),对于第 1 部分中的所有行,我们绘制了一个红线分隔符,而在其他部分中我们不显示任何内容。
关于ios - 为什么 TableView 在某些行上添加了不可见的分隔符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42293483/