2

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

import UIKit

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

我将其显示如下:

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
}

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

在此处输入图像描述

4

3 回答 3

9

使用故事板

UITableViewYou can dragUIView中,如果您有超过 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.

通过使用 XIB

class MyClass: UIView {

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

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

let FooterView = MyClass.instanceFromNib()
tableView.tableFooterView = FooterView

在此处输入图像描述

输出:

在此处输入图像描述

于 2018-08-31T06:54:01.597 回答
2

解释为什么你有空间距:它是由你的heightForFooterInSection方法引起的,它为 footerView 留下 100 像素。不幸的是,您还没有为您的 tableView 注册 FooterView 的 Nib,并且它不知道在这个地方要检索什么。

如何解决您的问题:ViewController您需要注册 FooterView 的 Nib 以供您的tableView.

EG:(请注意,您需要reuseIdentifier为您的 footerView 设置。

override func viewDidLoad() {
    super.viewDidLoad()
    tableView.register("FooterView", forHeaderFooterViewReuseIdentifier: "FooterView")
}

在此之后,您可以以与单元格类似的方式将 footerView 出列:

func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
    let footerView = tableView.dequeueReusableHeaderFooterView(withIdentifier: "FooterView")
    return footerView
}
于 2018-08-31T07:10:05.123 回答
-1
 func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {

     let myFooter =  Bundle.main.loadNibNamed("ReportFooterCell", owner: self, options: nil)?.first as! ReportFooterCell

    myFooter.toWorkHours?.text = toWorkHour.toString()
    myFooter.workedHours?.text = workedHour.toString()

     return myFooter
}

func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    return 40
}
于 2019-10-04T13:08:15.707 回答