我的情况非常特殊,想知道为什么会发生。我在 View 中有一个编辑框和按钮,将其放入 TableView 的 tableHeaderView 属性中。我将此 View 称为“InputToListCell”。
现在,当 View 扩展 UIView 时,我得到以下结果:
即我在代码中所做的更改是:
class InputToListCell: UIView{
//Code Here
}
然后,我更改 InputToListCell 以扩展 UITableViewCell 并获得以下内容:
即
class InputToListCell: UITableViewCell{
//Code Here
}
为什么会出现这种行为?我不明白为什么,因为 UITableViewCell 扩展了 UIView。有什么想法吗?
更新:
根据此处的评论,我们对 InputToListCell 进行了限制:
我基本上将编辑文本和添加按钮约束固定到边距,但编辑文本的尾随值除外。
最佳答案
简而言之
对表格 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
}
来自不同的帖子:
<小时/>有关如何使用 .storyboard
或 .xib
作为 header (包括 Xcode 项目)的完整示例,请参阅 https://stackoverflow.com/a/32261262/218152
关于ios - UIView 未填充 TableView tableHeaderView 属性 swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35021566/