tambahkan tombol di tampilan tabel footer dengan cepat

Saya ingin menambahkan tombol di footer tabel. Saya telah menulis kode ini tetapi tombolnya tidak berfungsi.

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
}

Terima kasih!

edit tableView kelas saya

impor UIKit

impor GoogleMobileAds

kelas 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

}

fungsi SetingTableViewControllerDidCancel(pengontrol : 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")

    }

}

}


person Zahed Ri    schedule 04.07.2015    source sumber
comment
Ada hasilnya atau tidak?   -  person Ashish Kakkad    schedule 04.07.2015
comment
tombol tidak berfungsi....tidak ada respons.   -  person Zahed Ri    schedule 04.07.2015
comment
Coba ini- dunamicButton.addTarget(self, action: Selector(buttonTouched:), forControlEvents: UIControlEvents.TouchUpInside)   -  person Amit89    schedule 04.07.2015


Jawaban (2)


Saya telah menguji kode Anda dan itu berfungsi dengan baik tetapi jika tidak berfungsi untuk Anda maka ada satu cara lagi untuk melakukannya, buka ViewController Anda di storyBoard lalu tambahkan tampilan baru di sel Anda seperti yang ditunjukkan di bawah ini:

masukkan deskripsi gambar di sini

Setelah itu Anda dapat menghubungkan tindakan tombol itu dengan kode ini:

@IBAction func btn(sender: AnyObject) {

    println("Working")
}

Periksa kode contoh INI. Ini akan bekerja dengan baik.

DI SINI adalah Contoh proyek dengan kode Anda.

person Dharmesh Kheni    schedule 04.07.2015
comment
tombol saya masih tidak berfungsi... tampilan tabel kelas saya sebagai berikut: class MenuViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, SetingTableViewControllerDelegate, UINavigationControllerDelegate { @IBOutlet lemah var tabelView: UITableView! - person Zahed Ri; 06.07.2015
comment
dapatkah Anda mengirimkan saya contoh proyek? - person Dharmesh Kheni; 06.07.2015
comment
@Dharmesh Kheni Terima kasih kode Anda berfungsi .. satu-satunya masalah adalah UIView ditampilkan di Awal ketika saya mencoba Memuat tampilan UItableview Terkait - person nivritgupta; 20.01.2016
comment
Bisakah Anda membagikan proyek demo Anda? - person Dharmesh Kheni; 20.01.2016

Ubah saja fungsi Anda menjadi action :

 @IBAction func buttonTouched(sender:UIButton!){
      println("Button was clicked", sender)
 }
person Ashish Kakkad    schedule 04.07.2015