TableView viewForHeaderInSection分隔线

时间:2018-10-24 12:55:34

标签: ios swift uitableview

我想将分隔线添加到tableForHeaderInSection中。当前,页眉节视图的代码为:

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    if cell.viewWithTag(kSeparatorId) == nil {
        let separatorView = UIView(frame: CGRect(x: 75, y: cell.frame.height - kSeparatorHeight, width: cell.frame.width-90, height: kSeparatorHeight))
        separatorView.tag = kSeparatorId
        separatorView.backgroundColor = UIColor(displayP3Red: 114/256, green: 112/256, blue: 133/256, alpha: 1.0)
        separatorView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        cell.addSubview(separatorView)
    }
}

输出:

[1]

但是我想要这个:

[2]

如何在viewForHeaderInSection处添加行。 谢谢。

1 个答案:

答案 0 :(得分:0)

创建分隔符视图...

func separatorView(width: CGFloat, inset: CGFloat, color: UIColor?) -> UIView {
    let footerView = UIView(frame: CGRect(x: 0, y: 0, width: width, height: 1))
    footerView.backgroundColor = color

    let insetView = UIView(frame: CGRect(x: footerView.bounds.origin.x, y: footerView.bounds.origin.y, width: inset, height: 1))
    insetView.backgroundColor = UIColor.white
    footerView.addSubview(insetView)
    return footerView
}

使用表格视图委托方法设置分隔符视图

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    return separatorView(width: tableView.frame.size.width, inset: tableView.separatorInset.left, color: tableView.separatorColor)
}
相关问题