我想知道,将页脚添加到tableview的最干净,最简单的方法是什么?
现在我进行硬编码:
func tableView(_ tableView: UITableView,
viewForFooterInSection section: Int) -> UIView? {
if viewModel.shouldShowPremiumBanner {
let footerView: LibraryFooterView = tableView.dequeueReusableHeaderFooter()
footerView.viewModel = LibraryFooterViewModel(navigator: Locator.navigator,
localStorage: Locator.localStorage)
self.tableView.tableFooterView = footerView
return footerView
}
return nil
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
if viewModel.shouldShowPremiumBanner {
return estimatedSectionFooterHeight
}
return .leastNormalMagnitude
}
我想将此添加到tableview中不
您已经通过设置 self.tableView.tableFooterView = footerView
根据文档
使用此属性可以为整个表指定页脚视图。页脚视图是出现在表视图滚动内容中的最后一项,它与您添加到各个部分的页脚视图分开。此属性的默认值为nil。
链接:https : //developer.apple.com/documentation/uikit/uitableview/1614976-tablefooterview
因此,只需删除您的viewForFooterInSection
代码,viewDidLoad
然后简单地设置您的viewController
override func viewDidLoad() {
super.viewDidLoad()
let footerView: LibraryFooterView = tableView.dequeueReusableHeaderFooter()
footerView.viewModel = LibraryFooterViewModel(navigator: Locator.navigator,
localStorage: Locator.localStorage)
self.tableView.tableFooterView = footerView
}
通过写入self.tableView.tableFooterView = footerView
,viewForFooterInSection
您可以简单地一次又一次地重置相同的变量,而没有实际的视觉变化,并且您一次又一次地创建view及其viewModel只是浪费了CPU周期。viewDidLoad
只要在您的情况下或在任何有意义的地方设置一次,就将其保留。
希望能帮助到你
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句