在swift中添加页脚tableview中的按钮

时间:2015-07-04 06:28:57

标签: xcode swift button tableview

我想在表格页脚中添加一个按钮。我写了这段代码,但按钮不起作用。

func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
    var footerView : UIView?
    footerView = UIView(frame: CGRectMake(0, 0, tableView.frame.size.width, 50))
    footerView?.backgroundColor = UIColor.blackColor()

    let dunamicButton = UIButton.buttonWithType(UIButtonType.System) as! UIButton
    dunamicButton.backgroundColor = UIColor.greenColor()
    dunamicButton.setTitle("Button", forState: UIControlState.Normal)
    dunamicButton.frame = CGRectMake(0, 0, 100, 50)
    dunamicButton.addTarget(self, action: "buttonTouched:", forControlEvents: UIControlEvents.TouchUpInside)

    footerView?.addSubview(dunamicButton)

    return footerView
}

func buttonTouched(sender:UIButton!){
println("diklik")
}

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

谢谢!

编辑我的类tableView

导入UIKit

导入GoogleMobileAds

类MenuViewController:UIViewController,UITableViewDataSource,UITableViewDelegate,SetingTableViewControllerDelegate,UINavigationControllerDelegate {

@IBOutlet weak var tabelView: UITableView!

 var data = ["BINATANG","BUAH","MAKANAN","SAYURAN","TUBUH","RUMAH","SEKOLAH","WARNA","ALAM","KENDARAAN","MAINAN"]



override func viewDidLoad() {

    super.viewDidLoad()

        self.tabelView.rowHeight = 100.0

}

func SetingTableViewControllerDidCancel(controller:SetingTableViewController)

{

    dismissViewControllerAnimated(true, completion: nil)

}

func SetingTableViewControllerDidDone(controller : SetingTableViewController){

    dismissViewControllerAnimated(true, completion: nil)



}

override func didReceiveMemoryWarning() {

    super.didReceiveMemoryWarning()

    // Dispose of any resources that can be recreated.

}



func tableView(tableView: UITableView,

    numberOfRowsInSection section: Int) -> Int {

    return data.count

}



func tableView(tableView: UITableView,

    cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = self.tabelView.dequeueReusableCellWithIdentifier("cell")

    as! KategoriCell



        cell.imgCell.image = UIImage(named: data1[indexPath.row]+"2")

        cell.labelCell.text = data[indexPath.row]



    return cell

}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

        if segue.identifier == "playId"{

        let indexPath = tabelView.indexPathForSelectedRow()



        let item = data[indexPath!.row]



        let detailViewController = segue.destinationViewController as! PlayViewController

        detailViewController.kategoriKe = indexPath!.row

        }

        else if segue.identifier == "settingId"{

            let navigationController = segue.destinationViewController

                as! UINavigationController

            let controller = navigationController.topViewController

                as! SetingTableViewController

             controller.delegate = self

        }

}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    var selectedCell:UITableViewCell = tableView.cellForRowAtIndexPath(indexPath)!

}

func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {

   NSUserDefaults.standardUserDefaults().setInteger(indexPath.row, forKey: "kategoriIdx")

}

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

    var height : CGFloat

    height = 50

    return height

}



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

    var footerView : UIView?

    footerView = UIView(frame: CGRectMake(0, 0, tableView.frame.size.width, 50))

    footerView?.backgroundColor = UIColor.blackColor()



    let dunamicButton = UIButton.buttonWithType(UIButtonType.System) as! UIButton

    dunamicButton.backgroundColor = UIColor.greenColor()

    dunamicButton.setTitle("Button", forState: UIControlState.Normal)

    dunamicButton.frame = CGRectMake(0, 0, 100, 50)

    dunamicButton.addTarget(self, action: "buttonTouched:", forControlEvents: UIControlEvents.TouchUpInside)



    footerView?.addSubview(dunamicButton)



    return footerView

}



func buttonTouched(sender:UIButton!){

    println("diklik")

}



func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {

    return 50.0

}



func navigationController(navigationController: UINavigationController,

    willShowViewController viewController: UIViewController,

    animated: Bool) {

    if viewController === self {

    NSUserDefaults.standardUserDefaults().setInteger(

    -1, forKey: "kategoriIdx")

    }

}

}

2 个答案:

答案 0 :(得分:1)

我已经测试了你的代码并且它工作正常但是如果它不适合你那么还有一种方法可以在storyBoard中转到你的ViewController然后添加一个新的视图来打击你的单元格,如下所示:

enter image description here

之后,您可以使用以下代码连接该按钮的操作:

@IBAction func btn(sender: AnyObject) {

    println("Working")
}

检查THIS示例代码。 这样可以正常工作。

HERE是带有您代码的示例项目。

答案 1 :(得分:0)

只需将您的功能更改为操作:

 @IBAction func buttonTouched(sender:UIButton!){
      println("Button was clicked", sender)
 }