ios - 如何在 Swift 中隐藏 UITableView 部分?

标签 ios uitableview swift

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

@IBOutlet var section: UITableViewSection!

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

section.hidden = true

不可以这样吗?

我的委托(delegate)和数据源设置 100% 正确。

最佳答案

swift 5:

您可以使用委托(delegate)方法heightForHeaderInSection :

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    if (section == 0) {
        return 0.0
    }
    return UITableView.automaticDimension
}

早于 Swift 5:使用 UITableViewAutomaticDimension 而不是 UITableView.automaticDimension

如果它不适用于高度 0.0,请使用高度 0.1

如果您不想在特定部分中放置任何单元格,请使用委托(delegate)方法:

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

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

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    switch section {
    case 0:
        return 0.0
    default:
        return UITableView.automaticDimension
    }
}

我对两者都进行了测试,它们工作正常。

关于ios - 如何在 Swift 中隐藏 UITableView 部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28947139/

相关文章:

ios - 如何从一个 View Controller 导航到另一个

ios - NavigationBar 在动画期间改变他的颜色

objective-c - IOS MapView 标注调整大小

ios - 删除动画期间行数更改时 UITableView 中的内部不一致

ios - 提取 NSDate() 的值

swift - 根据数组中的项目数迭代创建 UITextView

ios - Chartboost 代表无法快速工作

ios - CLGeocoder 可以在美国以外的全局范围内使用吗?

ios - 将 indexpath 存储在自定义 UITableViewCell 中

ios - 如何将 UIButtons 添加到我的 TableViewCell 中?