将多个页脚添加到UITableView

时间:2019-02-18 21:42:38

标签: ios swift uitableview

我想在我的UITableView中有两个页脚。我用它来添加页脚:https://stackoverflow.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,但仍然无法为我工作。

2 个答案:

答案 0 :(得分:0)

tableView.tableFooterView = customView2

覆盖第一个

tableView.tableFooterView = customView

您需要制作一个同时包含(customViewcustomView2的1个视图,然后将其分配为页脚


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

答案 1 :(得分:0)

根据Sh_Khan的帖子,您可以在viewForHeaderInSection中创建页脚,如下所示:

override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    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)
    customView.addSubview(button)

    let button2 = UIButton(frame: CGRect(x: 0, y: 0, width: 100, height: 50))
    button2.setTitle("Submit", for: .normal)
    customView.addSubview(button2)

    return customView
}

但是创建Xib并将其添加到viewForHeaderInSection中会更有用