ios - 在页脚上显示行数

标签 ios swift uitableview

我想对我的应用做一件简单的事情。 enter image description here

看看我的主 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)的区别:

enter image description here

enter image description here

最佳答案

只是实现

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/

相关文章:

iphone - 如何保护我的应用程序的一部分不受某些用户的影响(即,其中一个选项卡式 View 只能由某些用户看到)

ios - 关闭应用程序后禁用 UILocalNotifications

swift - 设置区域设置时 UIDatePicker 的时间位置错误

ios - 将图像设置为导航栏标题

ios - 不使用互联网、wifi、APNS 和苹果推送通知服务器的推送通知

ios - UITableView - 单元格图像在滚动时发生变化

ios - 添加新条目后如何重新加载 TableView ?

ios - Swift - 在一个 UIViewController 中实现 PageViewController

ios - 使用 Alamofire 进行基本身份验证

ios - 在表格单元格中添加到 UIView 的图形在滚动表格时表现得很奇怪