看看我的主 ViewController:
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]
}
}
}
那么,随着我添加越来越多的元素,我必须添加什么函数来显示行数?
有委托(delegate)和无委托(delegate)的区别:
最佳答案
只是实现
override func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
return "Total \(Shared.instance.employees.count) rows"
}
如果您想自定义标题,您必须实现 tableView:viewForFooterInSection:
并返回一个 View ,例如:
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
关于ios - 在页脚上显示行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43172433/