ios - TableView viewForHeaderInSection 分隔线

标签 ios swift uitableview

我想将分隔线添加到 tableForHeaderInSection 中。目前标题部分 View 的代码为:

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处添加行。 谢谢。

最佳答案

创建分隔符 View ...

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
}

使用table view委托(delegate)方法设置分隔符 View

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

关于ios - TableView viewForHeaderInSection 分隔线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52969583/

相关文章:

ios - 适用于 iOS 的 PDF 阅读器,支持从左到右分页

swift - IOS swift 如何为 Tableview 创建自定义第一个单元格

ios - 使用 UITableView 时应用程序崩溃

objective-c - 使用 UIControl(特别是 UIButton)时如何对目标进行排序?

ios - 如何使用 RxAlamofire 在帖子正文中传递 json 进行帖子调用

ios - 在 UINavigationController 中更改提示的字体

ios - iMessage 应用程序中的产品 ID 无效

ios - 我们如何在三列中拥有 TableViewCell

ios - 我如何(成功地)将 maskView 设置为 UIView?

ios - 避免在 CoreBluetooth 中重新连接时发现服务和特征