OGeek|极客世界-中国程序员成长平台

标题: ios - 在 UITableView 的节页脚中添加一个按钮和文本 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 20:12
标题: ios - 在 UITableView 的节页脚中添加一个按钮和文本

我正在尝试向 UITableView 中的节页脚添加一个可点击的按钮以及一些文本。我想获得与 iOS 设置应用程序完全相同的结果(如屏幕截图中的“了解更多...”按钮)。

有人知道苹果是怎么做到的吗?

enter image description here

编辑

抱歉,我添加的屏幕截图可能有点误导,因为我在按住按钮以使其更加突出时。这就是页脚实际的样子:

enter image description here

我尝试实现 titleForFooterInSection,然后将手势识别器添加到 willDisplayFooterView 中的标准页脚标签,但在初始调用时标签为 nil。我不确定这是否是一个好方法..

override func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
    return NSLocalizedString("This is some random text.", comment: "") + NSLocalizedString("Refresh", comment: "")
}

override func tableView(_ tableView: UITableView, willDisplayFooterView view: UIView, forSection section: Int) {

    guard let label = view.subviews[1].subviews.first as? UILabel else {
        return
    }
    let text = label.text!
    let underlineAttriString = NSMutableAttributedString(string: text)
    let range = (text as NSString).range(of: NSLocalizedString("Refresh", comment: ""))
    underlineAttriString.addAttribute(.foregroundColor, value: UIColor.blue, range: range)
    label.attributedText = underlineAttriString
    view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(refreshTapped)))
}



Best Answer-推荐答案


Jsut 使用 viewForHeaderInSection

 override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 150
    }
    override func tableView(_ tableView: UITableView, viewForFooterInSection  section: Int) -> UIView? {

        let header  = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 150));

            header.backgroundColor = UIColor.groupTableViewBackground
        let label = UILabel.init(frame: CGRect.init(x: 10, y: 10, width: tableView.frame.width-20, height: 100))
        label.text = "I tried using your code, but the footer doesn't look like the one from my screenshot. Here's what I got: Screenshot I usually work with storyboards and xibs so I don't really know how to align the text and button"
        label.numberOfLines = 4
        header.addSubview(label)

        let button = UIButton.init(frame: CGRect.init(x: ((tableView.frame.width-100) / 2), y: 90, width: 100, height: 40))
        button.setTitle("LoadMore...", for: UIControlState.normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 12)
        button.setTitleColor(.black, for: UIControlState.normal)
        button.backgroundColor = .white
        button.clipsToBounds = true
        button.layer.cornerRadius = 20
        header.addSubview(button)

        return header

    }

关于ios - 在 UITableView 的节页脚中添加一个按钮和文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50607419/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4