gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 05:21:04 29 4
gpt4 key购买 nike

我有一个包含 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/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com