看看我的主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]
}
}
}
那么,当我添加越来越多的itens时,我必须添加哪些函数来显示行数?
有和没有代表之间的差异:
答案 0 :(得分:4)
实施
override func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
return "Total \(Shared.instance.employees.count) rows"
}
如果要自定义标题,则必须实现tableView:viewForFooterInSection:
并返回视图,例如:
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
答案 1 :(得分:0)