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

时间:2016-08-08 08:46:32

标签: ios swift xcode tableview

如何在tableview标题中创建条形按钮或UIButton,当我点击tableview标题按钮时,它应该转到另一个Viewcontroller。我怎样才能通过Progarm实现这一目标。请帮助人

2 个答案:

答案 0 :(得分:4)

喜欢

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

最初获取Tableview的框架

let frame: CGRect = tableView.frame
  

为视图

中的UI按钮设置框架
let DoneBut: UIButton = UIButton(frame: CGRectMake(100, 0, 200, 50)) //frame.size.width - 60
DoneBut.setTitle("Done", forState: .Normal)
DoneBut.backgroundColor = UIColor.redColor()
  

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

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

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

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

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

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

调用方法如

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

}

更新回答

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

}

<强>输出

enter image description here

答案 1 :(得分:1)

大家好,这是有用的。可能对你有帮助。

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)            
}