问题
For 2 days I'm trying to solve this. I just want to add a footer (custom cell) to my tableview. I have a view with some stuff on it (labels, buttons) and I've added a tableview. To have a clean controller, for the datasource I'm using a separate file:
class MyDataSource: NSObject, UITableViewDataSource, UITableViewDelegate {
...
}
In my controller I have :
class MyViewController: UIViewController {
@IBOutlet weak var myButton: UIButton!
var myDatasource: MyDataSource!
@IBOutlet weak var myTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
myDatasource = MyDataSource(....)
myTableView.dataSource = myDatasource
// Do any additional setup after loading the view.
}
So, in MyDataSource I've added:
func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let footerView = UIView(frame: CGRectMake(0, 0, tableView.frame.size.width, 40))
footerView.backgroundColor = UIColor.blackColor()
return footerView
}
func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 40.0
}
And this piece of code is not being called.
I've looked the documentation and I've found that these 2 last methods are part of UITableViewController and not of part of UITableViewDataSource.
So, my question is, how to achieve that ?
Thanks.
C.C.
回答1:
As noted, displaying headers and footers needs to implement the relevant method of the UITableViewDelegate protocol. For the footer it is:
tableView(tableView: UITableView, viewForFooterInSection section: Int)
Make sure your table view's delegate is set correctly.
Also, note that there is another convenient table view feature: a footer that will be added below the entire table view, regardless of datasource or delegate implementation. Just assign a tableFooterView.
回答2:
Swift 3 (note underscores):
override func tableView( _ tableView: UITableView, viewForFooterInSection section: Int) -> UIView?
override func tableView( _ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat
来源:https://stackoverflow.com/questions/30000279/ios-8-swift-how-to-add-footer-for-tableview-using-separate-datasource