UIView没有填充表格视图tableHeaderView属性swift

Posted

技术标签:

【中文标题】UIView没有填充表格视图tableHeaderView属性swift【英文标题】:UIView not filling table views tableHeaderView property swift 【发布时间】:2016-01-26 18:52:16 【问题描述】:

我有一个非常特殊的情况,想知道为什么会这样。我在一个视图中有一个编辑框和按钮,我将其放入 tableview 的 tableHeaderView 属性中。我将此视图称为 InputToListCell。

现在,当视图扩展 UIView 时,我得到以下信息:

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

class InputToListCell: UIView
   //Code Here

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

class InputToListCell: UITableViewCell
   //Code Here

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

更新:

基于此处的评论是 InputToListCell 的约束:

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

【问题讨论】:

可能与如何在 InputToListCell 上设置自动布局约束有关。你能用一些相关信息更新你的问题吗? 请添加您的 cellForRowAtIndexPath 方法实现和单元格子视图的约束 建议的解决方案不适合您吗? 【参考方案1】:

简而言之

UITableViewCell 用于表格视图元素。

self.tableView.tableHeaderView =
    tableView.dequeueReusableCellWithIdentifier("sectionIdentifier")

讨论

对所有表格视图元素使用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

来自不同的帖子:


有关如何将 .storyboard.xib 用于标头(包括 Xcode 项目)的完整示例,请参阅 https://***.com/a/32261262/218152

【讨论】:

以上是关于UIView没有填充表格视图tableHeaderView属性swift的主要内容,如果未能解决你的问题,请参考以下文章

在Swift ios中方向更改为横向模式时隐藏底部表格视图

需要将视图添加为 TableHeader?

UIView 未调整大小以填充 iPhone 6 窗口

如何用另一个视图填充主 ViewController 上的 UIView?

uiview 作为包含 uitableview 的子视图

以 UIView 作为子视图创建的表格单元格在 iOS7 中不显示