Set custom table footer with Swift

I am trying to set a custom footer in my table view. I created a footer in the nib file and created a managed file for it.

class LoginTableFooter: UITableViewHeaderFooterView 

In viewDidLoad() I wrote this code

 let footerNib = UINib(nibName: "LoginTableFooter", bundle: nil) tableView.register(footerNib, forHeaderFooterViewReuseIdentifier: "LoginTableFooter") 

Then I implemented viewForFooterInSection

 func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? { let cell = self.tableView.dequeueReusableHeaderFooterView(withIdentifier: "LoginTableFooter") let header = cell as! LoginTableFooter return cell } 

viewForFooterInSection has never been called. I also tried to implement viewForHeaderInSection , but it was also not called. 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 to viewDidLoad ?

+5
source share
3 answers

Implement - delegate and data source for your view in the table and set both parameters - heightForFooterInSection and 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 } 
+7
source

Swift 3 Directly use> 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 } 
0
source

// first you need to call the delegate and data source in a table. 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" } 
0
source

Source: https://habr.com/ru/post/1264779/


All Articles