2015-07-04 5 views
1

Я хочу добавить кнопку в нижнем колонтитуле стола. Я написал этот код, но кнопка не работает.Добавить кнопку в нижнем колонтитуле в swift

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 

} 

функ 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") 

    } 

} 

}

+1

Любой результат был или нет? –

+0

кнопка нет работа .... нет ответ. –

+0

Попробуйте this- dunamicButton.addTarget (self, action: Selector ("buttonTouched:"), forControlEvents: UIControlEvents.TouchUpInside) – Amit89

ответ

1

Я проверил ваш код и он работает нормально, но если он не работает для вас, то есть еще один способ сделать это пойти к вашему ViewController в раскадровке затем добавить новый вид убирайте ячейку, как показано ниже:

enter image description here

После этого вы можете подключить действие этой кнопки с этим кодом:

@IBAction func btn(sender: AnyObject) { 

    println("Working") 
} 

Проверить THIS образец кода. Это будет нормально работать.

HERE Образец проекта с кодом.

+0

моя кнопка все еще не работает ... мой класс tableview выглядит следующим образом: class MenuViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, SetingTableViewControllerDelegate, UINavigationControllerDelegate { @IBOutlet слабый var tabelView: UITableView! –

+0

вы можете отправить мне образец проекта? –

+0

мой код выше .. –

0

Просто измените вашу функцию к действию:

@IBAction func buttonTouched(sender:UIButton!){ 
     println("Button was clicked", sender) 
} 
Смежные вопросы