我创建了一个 UIview
类 Xib
以在我的表格的页脚 View 中显示它。
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
}
这是怎么回事?我无法看到页脚 View ,也无法在 tableview
行之间获得如此大的空间。
最佳答案
使用 Storyboard
在UITableView
中可以拖动UIView
,如果原型(prototype)cell超过0个,它会设置为FooterView。拖动后,您可以在 TableView 层次结构中看到它作为 subview 。现在,您可以添加 UILabel
UIButton
或该 View 上的任何组件,调整高度等。您还可以将 IBAction
设置到 ViewController 类文件中.
以编程方式
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
}
}
初始化 View 并像下面那样使用它
let FooterView = MyClass.instanceFromNib()
tableView.tableFooterView = FooterView
输出:
关于ios - 如何在表格中显示自定义页脚 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52108871/