首页 > 解决方案 > 如何在swift 5中添加带有一个标签的表格视图页脚

问题描述

我只想在表格视图的末尾添加一个标签。

let customView = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 50))
customView.backgroundColor = UIColor.red
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 100, height: 50))
label.text = "app version"
customView.addSubview(label)

sideMenuTableView.tableFooterView = customView

我想将 customView 宽度设置为等于主视图的宽度,并且标签应该位于视图的最左角

标签: swiftswift5

解决方案


斯威夫特 5

添加 UITableViewDelegate 方法:-

    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.yellow
        let labelFooter = UILabel(frame: footerView.frame)
        labelFooter.text = "Footer view label text."
        labelFooter.textColor = .red
        footerView.addSubview(labelFooter)
        return footerView
    }

    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 40
    }

推荐阅读