在UITableview的页脚底部添加一个按钮

时间:2018-01-23 19:59:53

标签: ios swift uitableview

 @objc func hello(sender: UIButton!) {
print("Hello");

}
func tableView(_ tableView: UITableView, viewForFooterInSection 
section: Int) -> UIView? {

guard section == 0 else { return nil }

let footerView = UIView(frame: CGRect(0,0,130,0))
let DoneBut: UIButton = UIButton(frame: CGRect(10, 3, 300, 44))
DoneBut.setTitle("become a captian?", for: [])
DoneBut.backgroundColor = UIColor.lightGray
DoneBut.cornerRadius = 10
DoneBut.shadow = true
DoneBut.addTarget(self, action:#selector(hello(sender:)), for: .touchUpInside)
footerView.addSubview(DoneBut)
return footerView;

 }
 func tableView(_ tableView: UITableView, 
   estimatedHeightForFooterInSection section: Int) -> CGFloat {
   return 50
}

我正试图在页脚的底部中心DoneBut,但它没有发生!这就是我现在看到的

enter image description here

2 个答案:

答案 0 :(得分:2)

你应该做的是在创建按钮之后通过告诉什么是所需的原点来确定它,它可能是(注意它是 Swift 4 ):

func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {

    guard section == 0 else { return nil }

    let footerView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.width, height: 44.0))
    let doneButton = UIButton(frame: CGRect(x: 0, y: 0, width: 130, height: 44.0))
    // here is what you should add:
    doneButton.center = footerView.center

    doneButton.setTitle("become a captian?", for: .normal)
    doneButton.backgroundColor = .lightGray
    doneButton.layer.cornerRadius = 10.0
    doneButton.shadow = true
    doneButton.addTarget(self, action: #selector(hello(sender:)), for: .touchUpInside)
    footerView.addSubview(doneButton)

    return footerView
}

所以,如果你不能使用Swift,只需回顾一下你应该添加:

doneButton.center = footerView.center
声明按钮后

。通过将按钮center设置为与其容器视图(footerView)中心相同,它应该恰好位于其中间(x和y轴),这样您就可以获得所需的外观。

答案 1 :(得分:1)

aList = ['1','2','3','4','5']
index_to_remove = 0
del aList[index_to_remove]

enter image description here