ios 使用 Swift 设置自定义表格视图页脚

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42483976/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 08:56:33  来源:igfitidea点击:

Set a custom table view footer with Swift

iosswiftuitableview

提问by J.Doe

I am trying to set a custom footer to my table view. I created the footer on a nib file and created a controlled for it.

我正在尝试为我的表格视图设置自定义页脚。我在 nib 文件上创建了页脚并为其创建了一个控件。

class LoginTableFooter: UITableViewHeaderFooterView

In viewDidLoad()I wrote this code

viewDidLoad()我写了这段代码

let footerNib = UINib(nibName: "LoginTableFooter", bundle: nil)
        tableView.register(footerNib, forHeaderFooterViewReuseIdentifier: "LoginTableFooter")

Then I implemented viewForFooterInSection

然后我实现了 viewForFooterInSection

func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
    let cell = self.tableView.dequeueReusableHeaderFooterView(withIdentifier: "LoginTableFooter")
    let header = cell as! LoginTableFooter

    return cell
}

viewForFooterInSectionwas never called. I also tried to implement viewForHeaderInSectionbut it was also not called. Do you have an idea what is wrong? I have only one section in my table view; is it possible/better to set the footer directly on viewDidLoad?

viewForFooterInSection从未被调用。我也尝试实现,viewForHeaderInSection但也没有调用。你知道有什么问题吗?我的表格视图中只有一个部分;是否可以/更好地直接设置页脚viewDidLoad

回答by Krunal

Implement - delegate & datasource for your tableview and set both - heightForFooterInSection& viewForFooterInSection

实现 - 为您的 tableview 委托和数据源并设置 - heightForFooterInSection&viewForFooterInSection

tableView.delegate = self
tableView.dataSource = self

// set view for footer
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
    let footerView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 40))
    footerView.backgroundColor = UIColor.blue
    return footerView
}

// set height for footer
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    return 40
}

回答by Asaduzzaman Shuvro

//firstly you need to call the delegate and datasource of table view. i.e:

//首先你需要调用table view的delegate和datasource。IE:

  tableView.delegate = self
  tableView.dataSource = self

//you need to call this delegate

//你需要调用这个委托

     func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
      return "your expected footer height"
}

回答by Asaduzzaman Shuvro

Swift 3 Directly use > viewForFooterInSection

Swift 3 直接使用 > viewForFooterInSection

override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
    let footerView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 40))
    footerView.backgroundColor = UIColor.red

    return footerView
  }

回答by navin

func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
    let footerView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 40))
    footerView.backgroundColor = UIColor.red

    return footerView
  }