我正在使用 viewForHeaderInSection
来实现标题单元格。
我创建了一个单独的 View ,高度为 1,我假设它应该显示为分隔符。
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let cell = tableView.dequeueReusableCell(withIdentifier: "headerCell") as! CustomHeaderCell
cell.opt.text = "Some"
cell.len.text = "LEN"
cell.wgt.text = "WGT"
if !cell.constraintsInstalled {
let constraints = [
cell.stackView.leadingAnchor.constraint(equalTo: cell.contentView.leadingAnchor),
cell.stackView.trailingAnchor.constraint(equalTo: cell.contentView.trailingAnchor),
cell.stackView.topAnchor.constraint(equalTo: cell.contentView.topAnchor),
cell.stackView.bottomAnchor.constraint(equalTo: cell.contentView.bottomAnchor)
]
cell.contentView.addConstraints(constraints) //vw.addSubview(headerCell)
let separatorView = UIView(frame: CGRect(x: tableView.separatorInset.left, y: cell.frame.height, width: tableView.frame.width - tableView.separatorInset.right, height: 1))
separatorView.backgroundColor = UIColor.red
cell.addSubview(separatorView)
}
return cell
}
最佳答案
首先你应该把它添加到contentView
cell.contentView.addSubview(separatorView)
其次不要使用框架,因为单元格尚未放置,您还应该使用约束
separatorView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
separatorView.leadingAnchor.constraint(equalTo:cell.contentView.leadingAnchor),
separatorView.trailingAnchor.constraint(equalTo:cell.contentView.trailingAnchor),
separatorView.heightAnchor.constraint(equalToConstant:1),
separatorView.bottomAnchor.constraint(equalTo:cell.contentView.bottomAnchor)
])
关于ios - 红色分隔线未出现在 UITableViewCell 中的自定义标题下,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51689404/