ios 8 swift - 如何使用单独的数据源为tableview添加页脚 [英] ios 8 swift - how to add footer for tableview using separate datasource

查看:111
本文介绍了ios 8 swift - 如何使用单独的数据源为tableview添加页脚的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图解决这个问题2天。
我只想在我的tableview中添加一个页脚(自定义单元格)。
我有一些视图(标签,按钮),我添加了一个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 {
 ...   
}

在我的控制器里我有:

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
    }

这段代码不是调用。
我查看了文档,我发现这两个最后的方法是 UITableViewController 的一部分而不是 UITableViewDataSource <的一部分/ code>。

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 ?

谢谢。

CC

推荐答案

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

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.

另外,请注意还有另一个方便的表视图功能:将在整个表视图下添加一个页脚,而不管数据源或委托实现如何。只需指定一个 tableFooterView

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.

这篇关于ios 8 swift - 如何使用单独的数据源为tableview添加页脚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆