xcode 如何在 swift 2.2 的 tableview 标题中创建条形按钮或 UIButton?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38824804/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 09:07:36  来源:igfitidea点击:

how to create bar button or UIButton in tableview header in swift 2.2?

iosswiftxcodetableview

提问by anand

I want to create a UIBarButtonItemor UIButtonin UITableviewheader so when I click on the UITableviewheader button it should go to the other UIViewcontroller. How can I achieve this programmatically?

我想在标题中创建一个UIBarButtonItemUIButtonUITableview所以当我点击UITableview标题按钮时,它应该转到另一个UIViewcontroller。我怎样才能以编程方式实现这一目标?

采纳答案by Anbu.Karthik

do like

喜欢

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

initially get the frame of your Tableview

最初获取您的 Tableview 的框架

let frame: CGRect = tableView.frame

set the frame for UIbutton where is comes in View

设置 UIbutton 的框架,在 View 中

let DoneBut: UIButton = UIButton(frame: CGRectMake(100, 0, 200, 50)) //frame.size.width - 60
DoneBut.setTitle("Done", forState: .Normal)
DoneBut.backgroundColor = UIColor.redColor()

if you are using swift 2.2 call the selector as follows

如果您使用的是 swift 2.2,请按如下方式调用选择器

 DoneBut.addTarget(self, action: #selector(ViewController.buttonTapped(_:)), forControlEvents: .TouchUpInside)

if you are using swift 2.1 call the selector as follows

如果您使用的是 swift 2.1,请按如下方式调用选择器

DoneBut.addTarget(self, action: Selector("buttonTapped:"), forControlEvents: .TouchUpInside)button.addTarget(self, action: Selector("buttonTapped:"), forControlEvents: .TouchUpInside)

create the UIView reason viewforHeader returns UIView and add button to that subView

创建 UIView 原因 viewforHeader 返回 UIView 并将按钮添加到该子视图

 let headerView: UIView = UIView(frame: CGRectMake(0, 0, frame.size.width, frame.size.height))
headerView.addSubview(DoneBut)
return headerView
}

call method like

调用方法如

func buttonTapped(sender: UIButton) {
//Button Tapped and open your another ViewController

}

Updated answer

更新答案

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

    let frame: CGRect = tableView.frame


    let DoneBut: UIButton = UIButton(frame: CGRectMake(frame.size.width - 200, 0, 150, 50)) //
    DoneBut.setTitle("Done", forState: .Normal)
    DoneBut.backgroundColor = UIColor.redColor()

    DoneBut.addTarget(self, action: #selector(ViewController.buttonTapped(_:)), forControlEvents: .TouchUpInside)

    DoneBut.backgroundColor = UIColor.blueColor()

    let headerView: UIView = UIView(frame: CGRectMake(0, 0, frame.size.width, frame.size.height))
      headerView.backgroundColor = UIColor.redColor()
    headerView.addSubview(DoneBut)
    return headerView
}

func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 50.0
}

func buttonTapped(sender: UIButton) {
    //Button Tapped and open your another ViewController

}

output

输出

enter image description here

在此处输入图片说明

回答by anand

Hi guys this is working for.May be this helpful for you.

嗨,伙计们,这是有效的。可能对你有帮助。

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


func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {    
   let frame: CGRect = tableView.frame
   let DoneBut: UIButton = UIButton(frame: CGRectMake(200, 0, 200, 50))
   DoneBut.setTitle("+", forState: .Normal)
   DoneBut.backgroundColor = UIColor.blueColor()
   DoneBut.addTarget(self, action: #selector(DetailViewController.pressed(_:)), forControlEvents: .TouchUpInside)
   let headerView: UIView = UIView(frame: CGRectMake(0, 0, frame.size.height, frame.size.width))
   headerView.addSubview(DoneBut)
   return headerView
}


func pressed(sender: UIButton!) {
  print("Hello");    
  let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)    
  let qutationViewController = storyBoard.instantiateViewControllerWithIdentifier("newWorkViewController") as! NewWorkViewController
  self.navigationController?.pushViewController(qutationViewController, animated: true)            
}