如何在 table 中显示自定义页脚视图?
How to display custom footer view in table?
我创建了一个 UIview
class Xib
以在我的 table.
的页脚视图中显示它
import UIKit
/// This class is to display footer view in a settings table view
class FooterView: UIView {
override func awakeFromNib() {
super.awakeFromNib()
}
}
我显示如下:
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let view = FooterView(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 140))
return view
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 100
}
这有什么问题吗?我无法看到页脚视图并在 tableview
行之间看到这么多 space。
使用故事板
在UITableView
中可以拖动UIView
,如果原型cell超过0个就会设置为FooterView。拖动后,您可以在表视图层次结构中看到它作为子视图。现在,您可以在该视图上添加 UILabel
UIButton
或任何组件,调整高度等。您还可以将 IBAction
设置为 ViewController Class 文件。
以编程方式
let customView = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 50))
customView.backgroundColor = UIColor.red
let button = UIButton(frame: CGRect(x: 0, y: 0, width: 100, height: 50))
button.setTitle("Submit", for: .normal)
customView.addSubview(button)
//Add that view in Table Footer View.
tableView.tableFooterView = customView // Here you can also use your xib View.
通过使用 XIB
class MyClass: UIView {
class func instanceFromNib() -> UIView {
return UINib(nibName: "nib file name", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! UIView
}
}
初始化视图并像下面那样使用它
let FooterView = MyClass.instanceFromNib()
tableView.tableFooterView = FooterView
输出:
解释为什么你有空间距:这是由你的 heightForFooterInSection
方法造成的,它为 footerView 留下了 100 像素。不幸的是,您还没有为您的 tableView 注册 FooterView 的 Nib,它不知道在这个地方检索什么。
如何解决您的问题:
在您的 ViewController
中,您需要注册 FooterView 的 Nib 以供您的 tableView
使用。
例如:(请注意,您需要为 footerView 设置 reuseIdentifier
。
override func viewDidLoad() {
super.viewDidLoad()
tableView.register("FooterView", forHeaderFooterViewReuseIdentifier: "FooterView")
}
在此之后,您可以使用与单元格类似的方式使 footerView 出队:
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let footerView = tableView.dequeueReusableHeaderFooterView(withIdentifier: "FooterView")
return footerView
}
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let myFooter = Bundle.main.loadNibNamed("ReportFooterCell", owner: self, options: nil)?.first as! ReportFooterCell
myFooter.toWorkHours?.text = toWorkHour.toString()
myFooter.workedHours?.text = workedHour.toString()
return myFooter
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 40
}
我创建了一个 UIview
class Xib
以在我的 table.
import UIKit
/// This class is to display footer view in a settings table view
class FooterView: UIView {
override func awakeFromNib() {
super.awakeFromNib()
}
}
我显示如下:
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let view = FooterView(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 140))
return view
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 100
}
这有什么问题吗?我无法看到页脚视图并在 tableview
行之间看到这么多 space。
使用故事板
在UITableView
中可以拖动UIView
,如果原型cell超过0个就会设置为FooterView。拖动后,您可以在表视图层次结构中看到它作为子视图。现在,您可以在该视图上添加 UILabel
UIButton
或任何组件,调整高度等。您还可以将 IBAction
设置为 ViewController Class 文件。
以编程方式
let customView = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 50))
customView.backgroundColor = UIColor.red
let button = UIButton(frame: CGRect(x: 0, y: 0, width: 100, height: 50))
button.setTitle("Submit", for: .normal)
customView.addSubview(button)
//Add that view in Table Footer View.
tableView.tableFooterView = customView // Here you can also use your xib View.
通过使用 XIB
class MyClass: UIView {
class func instanceFromNib() -> UIView {
return UINib(nibName: "nib file name", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! UIView
}
}
初始化视图并像下面那样使用它
let FooterView = MyClass.instanceFromNib()
tableView.tableFooterView = FooterView
输出:
解释为什么你有空间距:这是由你的 heightForFooterInSection
方法造成的,它为 footerView 留下了 100 像素。不幸的是,您还没有为您的 tableView 注册 FooterView 的 Nib,它不知道在这个地方检索什么。
如何解决您的问题:
在您的 ViewController
中,您需要注册 FooterView 的 Nib 以供您的 tableView
使用。
例如:(请注意,您需要为 footerView 设置 reuseIdentifier
。
override func viewDidLoad() {
super.viewDidLoad()
tableView.register("FooterView", forHeaderFooterViewReuseIdentifier: "FooterView")
}
在此之后,您可以使用与单元格类似的方式使 footerView 出队:
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let footerView = tableView.dequeueReusableHeaderFooterView(withIdentifier: "FooterView")
return footerView
}
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let myFooter = Bundle.main.loadNibNamed("ReportFooterCell", owner: self, options: nil)?.first as! ReportFooterCell
myFooter.toWorkHours?.text = toWorkHour.toString()
myFooter.workedHours?.text = workedHour.toString()
return myFooter
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 40
}