ios - UIView 未填充 TableView tableHeaderView 属性 swift

标签 ios iphone swift uitableview uiview

我的情况非常特殊,想知道为什么会发生。我在 View 中有一个编辑框和按钮,将其放入 TableView 的 tableHeaderView 属性中。我将此 View 称为“InputToListCell”。

现在,当 View 扩展 UIView 时,我得到以下结果:

enter image description here

即我在代码中所做的更改是:

class InputToListCell: UIView{
   //Code Here
}

然后,我更改 InputToListCell 以扩展 UITableViewCell 并获得以下内容:

enter image description here

class InputToListCell: UITableViewCell{
   //Code Here
}

为什么会出现这种行为?我不明白为什么,因为 UITableViewCell 扩展了 UIView。有什么想法吗?

更新:

根据此处的评论,我们对 InputToListCell 进行了限制:

enter image description here

我基本上将编辑文本和添加按钮约束固定到边距,但编辑文本的尾随值除外。

最佳答案

简而言之

对表格 View 元素使用UITableViewCell

self.tableView.tableHeaderView =
    tableView.dequeueReusableCellWithIdentifier("sectionIdentifier")
<小时/>

讨论

对所有表格 View 元素使用 UITableViewCell 对象不仅可以在 UITableView 中呈现时提供伞式行为,而且还允许从 .storyboard< 加载.xib,额外的好处和凝聚力。

因此,即使 tableHeaderView (以及 viewForHeaderInSection )非正式地只是一个 UIView,您也可以返回一个 UITableViewCell 。根据this post ,要利用 tableHeaderView 中的 Autolayout,您需要做一些练习:

if let tableHeaderView = tableView.dequeueReusableCellWithIdentifier("tableHeaderViewIdentifier") {
    tableHeaderView.setNeedsLayout()
    tableHeaderView.layoutIfNeeded()

    let height = tableHeaderView.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize).height
    var frame = tableHeaderView.frame

    frame.size.height = height
    tableHeaderView.frame = frame
    self.tableView.tableHeaderView = tableHeaderView
}

来自不同的帖子:

Two Section Headers

<小时/>

有关如何使用 .storyboard.xib 作为 header (包括 Xcode 项目)的完整示例,请参阅 https://stackoverflow.com/a/32261262/218152

关于ios - UIView 未填充 TableView tableHeaderView 属性 swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35021566/

相关文章:

iphone - 如何使用 iOS 和 Objective-C/Xcode 加速 TDD 流程

ios - 使用ForEach删除动画的SwiftUI自定义列表不起作用

iphone - 如何检查我们在 iOS 中的特定应用程序的 GPS 权限是打开还是关闭?

swift - 如何将 Swift 3 中构建的 cocoapod 更新到 Swift 4

ios - 如何在进行搜索和过滤后使用 make a cell segue

swift - UIView 的动画方法没有出现

iOS:从字典中的.plist读取数据

ios - NetworkExtension VPN iOS 将蜂窝网络更改为 Wifi

iphone 方向问题..

ios - Swift/iOS - 在较小的屏幕上触发 ScrollView