找到你要的答案

Q:ios 8 swift - how to add footer for tableview using separate datasource

Q:iOS 8快速如何添加使用单独的DataSource TableView页脚

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.

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 {
 ...   
}

在我的控制器我有:

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.
    }

所以,在mydatasource我添加了:

 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.

所以,我的问题是,如何实现这一目标?

谢谢.

C.C.

answer1: 回答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.

如上所述,显示页眉和页脚需要实现的uitableviewdelegate协议相关的方法。对于页脚它是:

tableView(tableView: UITableView, viewForFooterInSection section: Int)

确保您的表视图的委托正确设置。

另外,请注意,还有一个方便的表观特征:页脚,将下面的整个表观补充道,无论数据源或委托实施。只指定一个tablefooterview。

answer2: 回答2:

Swift 3 (note underscores):

override func tableView( _ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? 

override func tableView( _ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat 

斯威夫特3(注意下划线):

override func tableView( _ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? 

override func tableView( _ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat 
ios  uitableview  swift  tableview  tableviewcell