【发布时间】:2018-07-09 12:25:08
【问题描述】:
我有一个设置屏幕,我想在我的 static UITableView 添加一个包含应用版本号的页脚:
我已经阅读了很多关于 SO 的帖子,其中绝大多数似乎是 Objective-C 和/或在 UITableView 中添加页脚,并使用 tableView(_:titleForHeaderInSection:) 和其他内容动态内容。我找到了one question 的答案,它建议只需使用UITableView.tableFooterView 属性,所以我也尝试了这个,但仍然没有如上图所示。
这是我所做的:
override func viewDidLoad() {
super.viewDidLoad()
setUpView()
}
func setUpView() {
self.navigationController?.navigationBar.topItem?.title = "Settings"
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(self.dismissSettings(_:)))
formulasTitle.textColor = UIColor.black
liftsTitle.textColor = UIColor.black
roundNumbersTitle.textColor = UIColor.black
sendFeedbackTitle.textColor = UIColor.black
unitsTitle.textColor = UIColor.black
pleaseRateTitle.textColor = UIColor.black
legalMumboJumboTitle.textColor = UIColor.black
currentFormulaSelection.detailTextLabel!.text = settingsViewModel.defaultFormulaName
currentLiftsSelection.detailTextLabel!.text = settingsViewModel.defaultLiftName
weightUnitControl.selectedSegmentIndex = settingsViewModel.weightUnitSelectedSegmentIndex
roundNumbersSwitch.isOn = settingsViewModel.roundNumbersSwitchOn
roundNumbersSwitch.addTarget(self, action: #selector(self.roundingOptionDidChange), for: .valueChanged)
weightUnitControl.addTarget(self, action: #selector(weightUnitDidChange), for: .valueChanged)
let versionLabel = UILabel()
versionLabel.text = getVersion()
let footer = UITableViewHeaderFooterView()
tableView.tableFooterView = footer
}
func getVersion() -> String {
let appVersion = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") ?? "0"
let appBuild = Bundle.main.object(forInfoDictionaryKey: "CFBundleVersion") ?? "0"
let versionLabelText: String
versionLabelText = "Version \(appVersion) (\(appBuild))"
return versionLabelText
}
使用tableView.tableFooterView 是正确的解决方案,我只是做错了,还是以其他方式添加页脚?
编辑#1
我已根据 Taras 的回答添加了此内容,但仍然没有:
override func viewDidLoad() {
super.viewDidLoad()
let versionLabel = UILabel()
versionLabel.text = getVersion()
let footer = UITableViewHeaderFooterView(reuseIdentifier: "footer")
tableView.tableFooterView = footer
tableView.register(UITableViewHeaderFooterView.self, forHeaderFooterViewReuseIdentifier: "footer")
footer.frame = CGRect(x: 50, y: 10, width: 100, height: 100)
footer.contentView.addSubview(versionLabel)
footer.backgroundView?.backgroundColor = UIColor.green
footer.constrainToSuperView()
}
extension UIView {
func constrainToSuperView() {
guard let superview = superview else {
return
}
translatesAutoresizingMaskIntoConstraints = false
topAnchor.constraint(equalTo: superview.topAnchor).isActive = true
bottomAnchor.constraint(equalTo: superview.bottomAnchor).isActive = true
leadingAnchor.constraint(equalTo: superview.leadingAnchor).isActive = true
trailingAnchor.constraint(equalTo: superview.trailingAnchor).isActive = true
}
}
需要注意的两个奇怪的事情:在运行时,footer 的框架是(50.0, 535.333333333333, 375.0, 100.0)。另外,如果我这样做,我会看到带有版本号的标签,但它是左对齐的,我似乎无法居中:
footer.textLabel?.text = versionLabel.text
【问题讨论】:
-
通过将其称为“静态”表,您是说它没有委托吗?您没有发布任何委托代码 - 如果您有委托,只需使用正确的覆盖即可。
-
“静态”是指我没有使用原型单元格,并且表格视图中的单元格数量永远不会改变。我实现的唯一委托函数是
tableView(UITableView, didSelectRowAt: IndexPath),用于确定在进行选择时使用哪个segue。 -
同意,@dtd。我仍然没有得到任何东西,但我非常接近。我更新了我的编辑以添加我确实实现但忘记包含在我的更新中的
heightForFooterInSection方法。我还按照您的建议删除了footerView上的框架,页脚视图看起来仍然不错。唯一的问题是UILabel没有出现。 -
感谢两位深思熟虑的人否决了我的问题。我看到关于 SO 的数百个问题打破了所有的提问规则——他们没有发布代码,不清楚,没有尝试任何自己解决问题的方法,有时只包含一个句子,比如“如何创建 UITableView ?” - 他们没有得到任何反对票。然而,我花了好几个小时研究我的问题,我问了这个非常合理的问题,我小心翼翼地尝试包含相关代码,但我得到了两个反对票,而对原因的解释为零。谢谢各位,真的,谢谢。你们真是英雄。
-
我知道不是你,@dfd - 你很有帮助。正如您在我的编辑中看到的那样,我正在根据您之前的建议跟踪您,因此我明确地将框架设置为
footer。但是,在运行时,当我打印footer和versionLabel的帧时,我得到Footer frame is (50.0, 535.333333333333, 375.0, 100.0)和Label frame is (0.0, 0.0, 0.0, 0.0)。我不明白页脚框架值是如何变化的。另外,我在这个 VC 的其余部分使用故事板和自动布局,但我在代码中做这个视图是为了了解更多关于这些东西是如何工作的。
标签: swift uitableview footer