[英]Set a custom table view footer with Swift
I am trying to set a custom footer to my table view. 我正在尝试为表格视图设置自定义页脚。 I created the footer on a nib file and created a controlled for it.
我在nib文件上创建了页脚,并为其创建了一个控件。
class LoginTableFooter: UITableViewHeaderFooterView
In viewDidLoad()
I wrote this code 在
viewDidLoad()
我编写了这段代码
let footerNib = UINib(nibName: "LoginTableFooter", bundle: nil)
tableView.register(footerNib, forHeaderFooterViewReuseIdentifier: "LoginTableFooter")
Then I implemented viewForFooterInSection
然后我实现了
viewForFooterInSection
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let cell = self.tableView.dequeueReusableHeaderFooterView(withIdentifier: "LoginTableFooter")
let header = cell as! LoginTableFooter
return cell
}
viewForFooterInSection
was never called. 从未调用过
viewForFooterInSection
。 I also tried to implement viewForHeaderInSection
but it was also not called. 我也尝试实现
viewForHeaderInSection
但也未调用它。 Do you have an idea what is wrong? 你有什么想法吗? I have only one section in my table view;
我的表格视图中只有一个部分; is it possible/better to set the footer directly on
viewDidLoad
? 是否可以/更好地直接在
viewDidLoad
上设置页脚?
Implement - delegate & datasource for your tableview and set both - heightForFooterInSection
& viewForFooterInSection
实现-代表与数据源为您的tableview和设置都-
heightForFooterInSection
& viewForFooterInSection
tableView.delegate = self
tableView.dataSource = self
// set view for footer
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let footerView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 40))
footerView.backgroundColor = UIColor.blue
return footerView
}
// set height for footer
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 40
}
Swift 3 Directly use > viewForFooterInSection Swift 3直接使用> viewForFooterInSection
override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let footerView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 40))
footerView.backgroundColor = UIColor.red
return footerView
}
//firstly you need to call the delegate and datasource of table view. //首先,您需要调用表视图的委托和数据源。 ie:
即:
tableView.delegate = self
tableView.dataSource = self
//you need to call this delegate //您需要呼叫此代表
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return "your expected footer height"
}
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let footerView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 40))
footerView.backgroundColor = UIColor.red
return footerView
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.