如何在表格中显示自定义页脚视图?

Kru*_*ala 2 uitableview ios tablefooterview swift

我创建了一个UIviewXib来在我的表格的页脚视图中显示它。页脚视图

import UIKit

/// This class is to display footer view in a settings table view
class FooterView: UIView {
    override func awakeFromNib() {
        super.awakeFromNib()
    }
}
Run Code Online (Sandbox Code Playgroud)

我将其显示如下:

func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
    let view = FooterView(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 140))
    return view
}

func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    return 100
}
Run Code Online (Sandbox Code Playgroud)

这有什么问题?我无法看到页脚视图并tableview在行之间获得如此多的空间。

在此处输入图片说明

Nik*_*ani 8

使用故事板

UITableViewYou can drag 中UIView,如果您有超过 0 个原型单元格,它将设置为 FooterView。拖动后,您可以在 tableview 层次结构中看到它作为子视图。现在,您可以UILabel UIButton在该视图上添加或任何组件,调整高度等。您也可以设置IBAction到 ViewController 类文件中。

以编程方式

let customView = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 50))
customView.backgroundColor = UIColor.red
let button = UIButton(frame: CGRect(x: 0, y: 0, width: 100, height: 50))
button.setTitle("Submit", for: .normal)
customView.addSubview(button)

//Add that view in Table Footer View.
tableView.tableFooterView = customView // Here you can also use your xib View.
Run Code Online (Sandbox Code Playgroud)

通过使用 XIB

class MyClass: UIView {

    class func instanceFromNib() -> UIView {
        return UINib(nibName: "nib file name", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! UIView
    }
}
Run Code Online (Sandbox Code Playgroud)

初始化视图并像下面一样使用它

let FooterView = MyClass.instanceFromNib()
tableView.tableFooterView = FooterView
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

输出:

在此处输入图片说明