如何在Swift中隐藏UITableViewSections?

nic*_*999 15 uitableview ios swift

我有一个静态分组表视图,有5个部分(所有部分都有标题,没有页脚).我使用Storyboard创建了所有这些.现在,我如何隐藏第一个/顶部UITableViewSection(包括标题).我尝试为UITableViewSection创建一个插座,但它告诉我它无效(未声明的类型):

@IBOutlet var section: UITableViewSection!
Run Code Online (Sandbox Code Playgroud)

我是这样做的,因为我打算这样做:

section.hidden = true
Run Code Online (Sandbox Code Playgroud)

不能这样做吗?

我的代理人和数据源已正确设置100%.

sas*_*tch 16

您可以使用委托方法:

override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  if(section == 0)
    return 0.0
}
Run Code Online (Sandbox Code Playgroud)

如果您不想在特定部分中使用单元格,请使用委托方法:

func numberOfRowsInSection(section: Int) -> Int {
  if (section == 0) {
    return 0
  }
  else {
  // return the number of rows you want
  }
}
Run Code Online (Sandbox Code Playgroud)

编辑(寻址Tim的评论)

要正确使用语法:

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    if (section == 0) {
        return 0.0
    }
    return UITableViewAutomaticDimension
}
Run Code Online (Sandbox Code Playgroud)

或者更简洁的switch-case语法:

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    switch section {
    case 0:
        return 0.0
    default:
        return UITableViewAutomaticDimension
    }
}
Run Code Online (Sandbox Code Playgroud)

我测试了两个,他们工作正常.


The*_*eil 7

我也希望你可以只@IBOutlet对一个部分进行隐藏并隐藏它,但遗憾的是它似乎没有,所以......

基于这里的各种建议,我已经建立了以下内容,它不需要对显式大小值进行任何干扰,并保留您可能已经在故事板/XIB 上设置的任何内容。它只是使您想要隐藏的任何部分的标题为零和行数为 0(导致大小为0.0)。

显然,您可以sectionShouldBeHidden根据需要进行配置;隐藏 #1 和 #3 只是任意示例。

斯威夫特 v5

private func sectionShouldBeHidden(_ section: Int) -> Bool {
    switch section {
    case 1, 3: return true
    default: return false
    }
}

override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    if sectionShouldBeHidden(section) {
        return nil // Show nothing for the header of hidden sections
    } else {
        return super.tableView(tableView, titleForHeaderInSection: section) // Use the default header for other sections
    }
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if sectionShouldBeHidden(section) {
        return 0 // Don't show any rows for hidden sections
    } else {
        return super.tableView(tableView, numberOfRowsInSection: section) // Use the default number of rows for other sections
    }
}
Run Code Online (Sandbox Code Playgroud)

更新:不幸的是,如果表格视图的样式是Plain. 当它是 时Grouped,每个部分之间还增加了额外的空间,这也需要照顾。这个额外的空间是节的页脚,所以可以这样处理:

override public func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    if sectionShouldBeHidden(section) {
        return CGFloat.leastNormalMagnitude // Use the smallest possible value for hidden sections
    } else {
        return super.tableView(tableView, heightForFooterInSection: section) // Use the default footer height for other sections
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 这个解决方案效果最好。它完全隐藏了各个部分,并且与已建立的 tableView 设置配合得很好! (2认同)

Jos*_*osé 6

我在这里尝试了所有解决方案,但都没有成功。最后,添加这些委托方法这个有效:

斯威夫特 5:

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return section == 4 ? 0 : return super.tableView(tableView, numberOfRowsInSection: section)
}

override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return section == 4 ? 0.1 : super.tableView(tableView, heightForHeaderInSection: section)
}

override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    return section == 4 ? 0.1 super.tableView(tableView, heightForFooterInSection: section)
}
Run Code Online (Sandbox Code Playgroud)

请注意,您需要返回 0.1 的高度,返回 0 不会这样做。