xcode 快速在页脚tableview中添加按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31217656/
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 07:24:59  来源:igfitidea点击:

add button in footer tableview in swift

xcodeswiftbuttontableview

提问by Zahed Ri

I want to add a button in a table footer. I have written this code but the button does not work.

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

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
}

Thanks!

谢谢!

edit my class tableView

编辑我的班级 tableView

import UIKit

导入 UIKit

import GoogleMobileAds

导入 Google 移动广告

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

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

func SetingTableViewControllerDidCancel(控制器: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")

    }

}

}

}

回答by Dharmesh Kheni

I have tested your code and it is working fine but if it is not working for you then there is one more way to do it go to your ViewController in storyBoard then add a new view blow your cell as shown below:

我已经测试了您的代码并且它运行良好,但是如果它对您不起作用,那么还有另一种方法可以在 storyBoard 中转到您的 ViewController,然后添加一个新视图来打击您的单元格,如下所示:

enter image description here

在此处输入图片说明

After that you can connect an action of that button with this code:

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

@IBAction func btn(sender: AnyObject) {

    println("Working")
}

Check THISsample code. This will work fine.

检查示例代码。这将正常工作。

HEREis Sample project with your code.

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

回答by Ashish Kakkad

Just change your function to action :

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

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