ios - 在 UITableView 的节页脚中添加一个按钮和文本
<p><p>我正在尝试向 UITableView 中的节页脚添加一个可点击的按钮以及一些文本。我想获得与 iOS 设置应用程序完全相同的结果(如屏幕截图中的“了解更多...”按钮)。 </p>
<p>有人知道苹果是怎么做到的吗?</p>
<p> <a href="/image/HYlr3.png" rel="noreferrer noopener nofollow"><img src="/image/HYlr3.png" alt="enter image description here"/></a> </p>
<p>编辑</p>
<p>抱歉,我添加的屏幕截图可能有点误导,因为我在按住按钮以使其更加突出时。这就是页脚实际的样子:</p>
<p> <a href="/image/yweZi.png" rel="noreferrer noopener nofollow"><img src="/image/yweZi.png" alt="enter image description here"/></a> </p>
<p>我尝试实现 titleForFooterInSection,然后将手势识别器添加到 willDisplayFooterView 中的标准页脚标签,但在初始调用时标签为 nil。我不确定这是否是一个好方法.. </p>
<pre><code>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.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)))
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>Jsut 使用 <code>viewForHeaderInSection</code></p>
<pre><code> override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 150
}
override func tableView(_ tableView: UITableView, viewForFooterInSectionsection: 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
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - 在 UITableView 的节页脚中添加一个按钮和文本,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/50607419/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/50607419/
</a>
</p>
页:
[1]