Ios 显示页脚上的行数
我想对我的应用程序做一件简单的事情。 看看我的主ViewController:Ios 显示页脚上的行数,ios,swift,uitableview,Ios,Swift,Uitableview,我想对我的应用程序做一件简单的事情。 看看我的主ViewController: class Page1: UITableViewController { override func numberOfSections(in tableView: UITableView) -> Int { return 1 } override func tableView(_ tableView: UITableView, numberOfRowsInSectio
class Page1: UITableViewController {
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return Shared.instance.employees.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell1
cell.nameLabel.text = Shared.instance.employees[indexPath.row].name
cell.positionLabel.text = Shared.instance.employees[indexPath.row].position
return cell
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destination = segue.destination as? Page2,
let indexPath = tableView.indexPathForSelectedRow {
destination.newPage = Shared.instance.employees[indexPath.row]
}
}
}
那么,当我添加越来越多的ITEN时,我需要添加什么函数来显示行数呢
有代表与无代表的区别:
只需执行
override func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
return "Total \(Shared.instance.employees.count) rows"
}
如果要自定义标题,必须实现tableView:viewForFooterInstitution:
并返回一个视图,例如:
override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let label = UILabel(frame: CGRect(x: 0, y: 0, width: tableView.frame.width, height: 30.0))
label.font = UIFont.boldSystemFont(ofSize: 20.0)
label.textAlignment = .center
label.text = "Total \(Shared.instance.employees.count) rows"
return label
}
旁注:不要多次调用Shared.instance.employees
而使用临时变量:
let employee = Shared.instance.employees[indexPath.row]
cell.nameLabel.text = employee.name
cell.positionLabel.text = employee.position
我解决了这个问题->我在原型单元格下面插入了一个简单的标签,如下所示: 然后,我把它放在
viewDidLoad
上:
footerLabel.text = String(Shared.instance.employees.count) + " employees"
顺便说一句,谢谢vadian先生的帮助。然后你必须实现
tableView:ViewForFooterInstitution:
,并返回一个自定义视图,该视图也可以是UILabel
。不,tableView:ViewForFooterInstitution:
是一个类似tableView:TitleForFooterInstitution:
的委托方法。您必须返回一个UIView
实例,该视图可以包含您喜欢的任何内容,但您要负责外观(大小、字体、对齐方式等)。字符串不是视图,UIView
和string
根本不相关。我更新了答案。确实,您必须选择一种委托方法,但是如果实现了viewforfooterInstition
,则忽略titleforfooterInstition
。如果实现了viewforfooterInstition
,则还必须实现heightforfooterInstition
。