Ios 如何在UITableView节之间添加分隔符?

Ios 如何在UITableView节之间添加分隔符?,ios,swift,uitableview,cocoa-touch,uikit,Ios,Swift,Uitableview,Cocoa Touch,Uikit,我正在实现一个tableview,它有3个部分(部分计数保持静态),每个部分包含几行 行为1:仅将节标题添加到第三节 我是如何做到的: 行为2:TableViewCells不应具有分隔符。但是,应使用细线分隔各部分。(类似于tableviewcell分隔符) 我尝试了什么: 添加一个单像素高度、tableView宽度和UIView,并设置所有必需的属性(颜色等),然后将其作为子视图添加到节标题视图的 将带有CGPoint(0,1)的阴影添加到剖面标头视图中 他们都没有成功 我是如何做到这一点

我正在实现一个tableview,它有3个部分(部分计数保持静态),每个部分包含几行

行为1:仅将节标题添加到第三节

我是如何做到的:

行为2:TableViewCells不应具有分隔符。但是,应使用细线分隔各部分。(类似于tableviewcell分隔符)

我尝试了什么:

  • 添加一个单像素高度、tableView宽度和UIView,并设置所有必需的属性(颜色等),然后将其作为子视图添加到节标题视图的
  • 将带有CGPoint(0,1)的阴影添加到剖面标头视图中
他们都没有成功

我是如何做到这一点的:

  • 找到每个部分的最后一个单元格
  • 创建一个具有1px高度、tableView宽度的UIView,并将其作为子视图添加到最后一个单元格中

  • 虽然这似乎有效。我觉得,必须有更好的方法来实现这一点,而不是在每个部分的最后一个tableView单元格的底部添加行。有人知道更好的方法来实现这种行为吗?

    使用
    UITableViewDelegate的
    heightForHeaderInSection
    方法,并根据需要返回分段高度

    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
            if section == 2 {
                return 1 // return height as per your requirement
            }
            return 0 // return height as per your requirement
        }
    

    这适用于添加“剖面分隔线”和仅在第三个剖面上的俯视图:

    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        // if it's the 3rd section, return a view with desired content for the section header
        if section == 2 {
            let v = UIView(frame: CGRect(x: 0, y:0, width: tableView.frame.width, height: 30))
            v.backgroundColor = .yellow
            let label = UILabel(frame: CGRect(x: 8.0, y: 4.0, width: v.bounds.size.width - 16.0, height: v.bounds.size.height - 8.0))
            label.autoresizingMask = [.flexibleWidth, .flexibleHeight]
            label.text = "Header for Third Section"
            v.addSubview(label)
            return v
        }
        // not the 3rd section, so don't return a view
        return nil
    }
    
    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        // if it's the 3rd section
        if section == 2 {
            return 30
        }
        return 0
    }
    
    override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        // UIView with darkGray background for section-separators as Section Footer
        let v = UIView(frame: CGRect(x: 0, y:0, width: tableView.frame.width, height: 1))
        v.backgroundColor = .darkGray
        return v
    }
    
    override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        // Section Footer height
        return 1.0
    }
    

    将表格视图的样式设置为“分组”:

    简单的解决方案就是在表格视图中添加页脚视图。将页脚视图的高度设置为特定的高度

    斯威夫特:

     public func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        let view = UIView()
        view.backgroundColor = UIColor.grey
        return view
    }
    
    public func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 10
    }
    

    这真的奏效了。谢谢我想了解一下autoresizingMask属性。为什么我们需要设置它?理解它的最好方法就是玩它。将背景色添加到标题中的标签:
    label.backgroundColor=.cyan
    ,以便您可以看到其框架。。。然后注释掉
    label.autoresizingMask=
    行。。。运行应用程序时,您将看到标签框是从完整的页眉视图框中插入的。现在,旋转设备。如果没有
    .autoresizingMask
    设置,标签不会“自动调整大小”到表格的新宽度。您也可以使用约束——如果它是一个复杂的标题,这可能就是您想要做的。
     public func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        let view = UIView()
        view.backgroundColor = UIColor.grey
        return view
    }
    
    public func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 10
    }