【问题标题】:How to display custom footer view in table?如何在表格中显示自定义页脚视图?
【发布时间】:2018-08-31 05:19:46
【问题描述】:

我创建了一个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 行之间获得了如此多的空间。

【问题讨论】:

  • 给我们看FooterView类的代码。
  • 在创建自定义页脚视图时继承UITableViewHeaderFooterView。并使用dequeueReusableHeaderFooterViewWithIdentifier 初始化它
  • @NiravD 我没有在 Footerview 类中进行任何编码。它只是 xib。类继承自 UIView。已添加有问题的代码。
  • @QuocNguyen UITableViewHeaderFooterView 不允许在 xib 中设置类名。
  • @KrutikaSonawala,为什么你需要使用 xib,让我知道对于 tableview,你使用 viewController 吗?

标签: ios swift uitableview tablefooterview


【解决方案1】:

使用故事板

UITableView你可以拖动UIView,如果你有超过0个原型单元格,它将设置为FooterView。拖动后,您可以在 tableview 层次结构中将其视为子视图。现在,您可以在该 View 上添加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

输出:

【讨论】:

  • 哇!太感谢了!我没有使用 instanceFromNib。有必要吗?还有其他方法吗?
  • @KrutikaSonawala 非常欢迎 :) 是的,当您使用与标识符相同的 xib 时,这是必要的。
【解决方案2】:

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

如何解决您的问题: 在您的 ViewController 中,您需要注册 FooterView 的 Nib 以供您的 tableView 使用。

例如:(请注意,您需要为您的 footerView 设置 reuseIdentifier

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
}

【讨论】:

    【解决方案3】:
     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
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-26
      • 2017-03-21
      • 2012-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多