向 UITableView 添加多个页脚
Posted
技术标签:
【中文标题】向 UITableView 添加多个页脚【英文标题】:Add multiple footers to UITableView 【发布时间】:2019-02-18 21:42:37 【问题描述】:我想在我的UITableView
中有两个页脚。我用它来添加页脚:https://***.com/a/38178757/10466651
我尝试在viewDidLoad
中添加两个这样的页脚:
let customView = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 50))
customView.backgroundColor = UIColor.red
let button = UIButton(frame: CGRect(x: 0, y: 0, width: 100, height: 50))
button.setTitle("Submit", for: .normal)
button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
customView.addSubview(button)
tableView.tableFooterView = customView
let customView2 = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 50))
customView2.backgroundColor = UIColor.red
let button2 = UIButton(frame: CGRect(x: 0, y: 0, width: 100, height: 50))
button2.setTitle("Submit", for: .normal)
button2.addTarget(self, action: #selector(buttonAction2), for: .touchUpInside)
customView2.addSubview(button2)
tableView.tableFooterView = customView2
但它只是互相超越。我尝试使用CGRect y
,但仍然无法为我工作。
【问题讨论】:
基本上,你想把两个按钮放到footerview中? 我在考虑将两个按钮作为页脚(或者如果可能的话,使用 2 个页脚?) 可以先画一个containerView
,设置成footerView
,然后把两个按钮放进去
tableView.tableFooterView = customView tableView.tableFooterView = customView2 这里的“=”是赋值运算符。并且您首先分配 customView1,然后分配 customView2。它肯定不会以这种方式添加两个表脚。您将需要创建一个页脚视图(因为 tableview 只能采用一个)并在该容器视图中添加您的按钮或任何其他视图并将其分配给 tableview
【参考方案1】:
这个
tableView.tableFooterView = customView2
覆盖第一个
tableView.tableFooterView = customView
您需要制作 1 个包含两者 (customView
+ customView2
) 的视图,然后将其指定为页脚
let customView = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 115))
customView.backgroundColor = UIColor.red
let button = UIButton(frame: CGRect(x: 0, y: 0, width: 100, height: 50))
button.setTitle("Submit 1", for: .normal)
button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
customView.addSubview(button)
let button2 = UIButton(frame: CGRect(x: 0, y: 65, width: 100, height: 50))
button2.setTitle("Submit 2", for: .normal)
button2.addTarget(self, action: #selector(buttonAction2), for: .touchUpInside)
customView.addSubview(button2)
tableView.tableFooterView = customView
【讨论】:
谢谢!如何在按钮之间添加小间距?以上是关于向 UITableView 添加多个页脚的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Storyboard 中向 UITableView 添加页脚
如何在 splitViewController 的 RootViewController 中将页脚图像添加到 UITableView