ios - 在 Swift 中设置 tableView header 的高度

标签 ios swift xcode uitableview uiview

我正在尝试在 TableView Controller 中设置位于我的原型(prototype)单元格顶部的 View 的高度。我使用 IB 来设置它的高度(尺寸检查器)并将它设置为 61,就像这样(绿色 View 是“标题” View ):

header view

但是每当我运行该应用程序时,它的高度最终都会变为 568.0。我的 TableView Controller 中的 View 有一个名为 testUIView 的 IBOutlet,我这样做:println("testUIView Height->\(testUIView.frame.height)")并且在运行时确实最终成为 568.0

这是显示其运行时高度的屏幕截图:

enter image description here

所以我的问题是:如何设置 View 的高度,使其在运行时为 61,使其看起来确实像我的第一个屏幕截图(按大小)?

我试图在 override func viewWillLayoutSubviews() 中设置它的高度属性,但它不允许我为高度赋值 testUIView.frame.height = CGFloat(61.0).

感谢任何帮助!提前致谢!

干杯!

最佳答案

这是一个使用节标题 View 而不是实际表标题 View 的解决方案:


如果您想为 UITableView 使用 header ,您可以在 Interface Builder 中设计另一个原型(prototype)单元格,基于 UITableViewCell 创建一个自定义类,然后在类检查器上将其分配给 Interface Builder 中的原型(prototype)单元格。

然后在你的 Controller 中你将要使用

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?

在该函数中,您实际上将从表格 View 中创建一个可重用的单元格,但将其转换为您为标题创建的自定义单元格。您将可以像普通 UITableViewCell 一样访问它的所有属性,然后您只需返回单元格的 View

return cell.contentView

另一种方法是

func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 61.0
}

这个是不言自明的。

swift 3.0.1

public override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 61.0
}

关于ios - 在 Swift 中设置 tableView header 的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31665927/

相关文章:

objective-c - Objective-C : How to add color to entire cell (with a disclosure indicator)

javascript - 在react-native中获取主颜色?

ios - 在 uitableview 编辑模式下丢弃 segue

ios - Swift 2.0 格式 100 0's into a friendly K' s

ios - Xcode 6 Storyboard Unwind Segue with Swift 不连接退出

swift - 如何在其他 View 中使用 Swift 中的 AVPlayer 暂停和播放音乐

ios - 来自字符串的日期未在 UIDatePicker Swift 3 中设置

ios - Interface Builder 中具有空 UIStackView 的 UIScrollView 的约束错误

ios - 选择 UICollectionViewCell 并获取 indexPath 可以通过这种方式完成吗?

ios - @导入X;导致错误 Could not build module X