【问题标题】:How to add separators between UITableView sections?如何在 UITableView 部分之间添加分隔符?
【发布时间】:2018-08-24 05:56:51
【问题描述】:

我正在实现一个表格视图,它有 3 个部分(部分计数保持不变),每个部分包含几行。

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

我是如何做到的:

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    if section == 2 {
       //create and return header view
    }
    return UIView(frame: CGRect(x: 0, y:0, width: tableView.width, height: UIScreen.main.scale))
}

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

我做了什么:

  • 添加一个像素高度、tableView 宽度的 UIView,并设置所有必需的属性(颜色等),并将其作为子视图添加到部分标题视图的
  • 使用 CGPoint(0,1) 将阴影添加到节标题视图

他们都没有成功。

我是如何做到的:

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

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

【问题讨论】:

    标签: ios swift uitableview cocoa-touch uikit


    【解决方案1】:

    这个简单的解决方案只需在tableview中添加页脚视图。将页脚视图的高度设置为特定高度。

    斯威夫特:

     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
    }
    

    【讨论】:

      【解决方案2】:

      使用UITableViewDelegateheightForHeaderInSection 方法并根据需要返回分段高度

      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
          }
      

      【讨论】:

        【解决方案3】:

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

        【讨论】:

          【解决方案4】:

          这应该适用于在第三部分添加“部分分隔线”和头部视图

          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
          }
          

          【讨论】:

          • 确实有效。谢谢!!我想了解 autoresizingMask 属性。为什么我们需要设置它?
          • 理解它的最好方法是玩它。为标题中的标签添加背景颜色:label.backgroundColor = .cyan 这样您就可以看到它的框架...然后注释掉label.autoresizingMask = 行...当您运行应用程序时,您会看到标签框架是嵌入的从完整的标题视图框架。现在,旋转设备。如果没有 .autoresizingMask 设置,标签不会“自动调整大小”为表格的新宽度。您也可以使用约束——如果它是一个复杂的标题,那可能就是您想要做的。
          猜你喜欢
          • 1970-01-01
          • 2015-12-08
          • 1970-01-01
          • 1970-01-01
          • 2011-03-02
          • 2012-10-14
          • 2013-08-20
          • 1970-01-01
          • 2012-12-06
          相关资源
          最近更新 更多