2015-05-07 1 views
6

Nella mia pagina principale, ho creato un file xib per UITableViewCell. Sto caricando la cella da quel file xib e sta funzionando bene.Come aggiungere un pulsante con evento click su UITableViewCell in Swift?

All'interno della cella ho alcune etichette e pulsanti. Sto mirando a cambiare l'etichetta facendo clic sul pulsante sulla cella.

My Code ama sotto

import UIKit 


class SepetCell: UITableViewCell{ 

    @IBOutlet var barcode: UILabel! 
    @IBOutlet var name: UILabel! 
    @IBOutlet var fav: UIButton! 
    @IBOutlet var strep: UIStepper! 
    @IBOutlet var times: UILabel! 



    @IBAction func favoriteClicked(sender: UIButton) { 
     println(sender.tag) 
     println(times.text) 

     SepetViewController().favorite(sender.tag) 



    } 
    override func awakeFromNib() { 
     super.awakeFromNib() 
     // Initialization code 
    } 

    override func setSelected(selected: Bool, animated: Bool) { 
     super.setSelected(selected, animated: animated) 

     // Configure the view for the selected state 
    } 
} 

Questo è il mio file XI ter dietro codici come .swift.

I codici inseriti nella pagina principale del piace di seguito:

import UIKit 
import CoreData 

class SepetViewController: UIViewController, UITextFieldDelegate, UITableViewDataSource, UITableViewDelegate { 

    @ 
    IBOutlet 
    var sepetTable: UITableView! 
    var barcodes: [CART] = [] 

    let managedObjectContext = (UIApplication.sharedApplication().delegate as!AppDelegate).managedObjectContext 

    override func viewWillAppear(animated: Bool) { 
    if let moc = self.managedObjectContext { 


     var nib = UINib(nibName: "SepetTableCell", bundle: nil) 
     self.sepetTable.registerNib(nib, forCellReuseIdentifier: "productCell") 
    } 
    fetchLog() 
    sepetTable.reloadData() 
    } 

    func fetchLog() { 

    if let moc = self.managedObjectContext { 
     barcodes = CART.getElements(moc); 
    } 
    } 



    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) - > Int { 
    return self.barcodes.count 
    } 

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) - > UITableViewCell { 


    let cell = tableView.dequeueReusableCellWithIdentifier("productCell") as ? SepetCell 


    if cell == nil { 
     println("cell nil") 

    } 



    let product: CART 
    product = barcodes[indexPath.row] 



    cell!.barcode ? .text = product.barcode 
    cell!.name ? .text = product.name 
    cell!.fav.tag = indexPath.row 

    return cell! 
    } 

    func favorite(tag: Int) { 



    } 
} 

quando ho cliccato il pulsante FAV all'interno della cellula. Ad esempio, volevo cambiare il testo dell'etichetta del tempo in qualsiasi cosa.

Quando ho fatto clic sul pulsante fav, l'evento è passato alla funzione SepetCell.swift preferitaClicker (mittente: UIButton).

Quindi, se provo a chiamare:. SepetViewController() preferito (sender.tag)

Si andrà all'interno della

func favorite(tag: Int) { 

     sepetTable.reloadData() 

    } 

ma sepetTable è nullo quando è andato lì. Penso che sia a causa di quando chiamo questa funzione di SepetViewController(). Favorite (sender.tag). Crea innanzitutto la classe SepetViewController. Quindi, dato che l'oggetto non è settato, sta diventando nullo.

Come posso raggiungere quel settetTable o qual è il modo migliore per risolvere questo problema.

Grazie.

risposta

28

che definirei una chiusura nella classe Cell:

class SepetCell: UITableViewCell{ 
    var onButtonTapped : (() -> Void)? = nil 

poi

@IBAction func favoriteClicked(sender: UIButton) { 
     println(sender.tag) 
     println(times.text) 
     if let onButtonTapped = self.onButtonTapped { 
      onButtonTapped() 
     } 
    } 

poi nel vostro delegato Tableview:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) - > UITableViewCell { 

    let cell = tableView.dequeueReusableCellWithIdentifier("productCell") as ? SepetCell 
    cell.onButtonTapped = { 
     //Do whatever you want to do when the button is tapped here 
    } 

Spero che questo aiuti!

+0

grazie mille. ha funzionato per me. Se voglio inviare qualcosa da SepetCell a tableView cosa devo fare? Ad esempio, ho uno strepper e quando clicco su di esso voglio cambiare il valore dell'etichetta –

+2

ok l'ho trovato con qualche tentativo :) Ho apportato alcune modifiche al codice come: var onFavButtonTapped: ((Int) - > Vuoto)? = Nil **** se lasciare onButtonTapped = {self.onButtonTapped onButtonTapped (3) } **** cella .onButtonTapped =! {(X: Int) in println (x) } –

+0

bello sentire !. A proposito, se hai trovato la mia risposta utile, si prega di considerare upvoting :) – raf