เพิ่มปุ่มในส่วนท้าย 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, การตั้งค่าTableViewControllerDelegate, 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 การตั้งค่าTableViewControllerDidCancel (ตัวควบคุม: การตั้งค่าTableViewController)

{

    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 แหล่งที่มา
comment
ได้ผลมาหรือเปล่า?   -  person Ashish Kakkad    schedule 04.07.2015
comment
ปุ่มไม่ทำงาน....ไม่มีการตอบกลับ   -  person Zahed Ri    schedule 04.07.2015
comment
ลองสิ่งนี้ - dunamicButton.addTarget(self, action: Selector(buttonTouched:), forControlEvents: UIControlEvents.TouchUpInside)   -  person Amit89    schedule 04.07.2015


คำตอบ (2)


ฉันได้ทดสอบโค้ดของคุณแล้วและใช้งานได้ดี แต่หากไม่ได้ผลสำหรับคุณ มีอีกวิธีหนึ่งที่จะทำได้โดยไปที่ ViewController ของคุณใน storyBoard จากนั้นเพิ่มมุมมองใหม่ให้เซลล์ของคุณดังที่แสดงด้านล่าง:

ป้อนคำอธิบายรูปภาพที่นี่

หลังจากนั้นคุณสามารถเชื่อมต่อการทำงานของปุ่มนั้นด้วยรหัสนี้:

@IBAction func btn(sender: AnyObject) {

    println("Working")
}

ตรวจสอบโค้ดตัวอย่าง นี้ สิ่งนี้จะทำงานได้ดี

ที่นี่คือโปรเจ็กต์ตัวอย่างที่มีโค้ดของคุณ

person Dharmesh Kheni    schedule 04.07.2015
comment
ปุ่มของฉันยังคงใช้งานไม่ได้... มุมมองตารางคลาสของฉันดังนี้: class MenuViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, SetingTableViewControllerDelegate, UINavigationControllerDelegate { @IBOutlet var tabelView ที่อ่อนแอ: UITableView! - person Zahed Ri; 06.07.2015
comment
คุณช่วยส่งโครงการตัวอย่างมาให้ฉันได้ไหม - person Dharmesh Kheni; 06.07.2015
comment
@Dharmesh Kheni ขอบคุณรหัสของคุณใช้งานได้ .. ปัญหาเดียวคือ UIView แสดงบนการเริ่มต้นเมื่อฉันพยายามโหลดมุมมอง UItable ที่เกี่ยวข้อง - person nivritgupta; 20.01.2016
comment
คุณสามารถแบ่งปันโครงการสาธิตของคุณได้หรือไม่? - person Dharmesh Kheni; 20.01.2016

เพียงเปลี่ยนฟังก์ชั่นของคุณเป็นการกระทำ:

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