gpt4 book ai didi

ios - 将按钮和文本添加到 UITableView 中的节页脚

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:02:48 24 4
gpt4 key购买 nike

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

有谁知道 Apple 是怎么做到的?

enter image description here

编辑

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

enter image description here

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

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)))
}

最佳答案

只要使用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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com