2015-05-07 14 views
6

Na mojej stronie głównej utworzyłem plik Xib dla UITableViewCell. Ładuję komórkę z tego pliku xib i działa poprawnie.Jak dodać przycisk z zdarzeniem kliknięcia na UITableViewCell w Swift?

Wewnątrz komórki mam etykiety i przyciski. Zamierzam zmienić etykietę, klikając przycisk w komórce.

mój kod lubi poniżej

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 
    } 
} 

To jest moje pliki XIb za kody jak .swift.

Kody na stronę główną lubi poniżej:

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



    } 
} 

Po kliknięciu przycisku fav wewnątrz komórki. Chciałem zmienić czas etykietowania tekstu na cokolwiek, na przykład.

Po kliknięciu przycisku fav, wydarzenie przejdzie do funkcji SepetCell.swift favoriteClicked (sender: UIButton).

Więc jeśli próbuję zadzwonić. SepetViewController() ulubiony (sender.tag)

To pójdzie wewnątrz

func favorite(tag: Int) { 

     sepetTable.reloadData() 

    } 

ale sepetTable jest zerowa, gdy jest on tam poszedł. Myślę, że dzieje się tak z powodu wywołania tej funkcji SepetViewController(). Favorite (sender.tag). Najpierw tworzy klasę SepetViewController. Ponieważ obiekt nie jest ustawiony, otrzymuje wartość null.

Jak mogę osiągnąć ten sepetTable lub jaki jest najlepszy sposób rozwiązania tego problemu.

Dzięki.

Odpowiedz

28

ja bym zdefiniował zamknięcie w klasie komórkowy:

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

następnie

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

następnie w delegata 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 
    } 

nadzieję, że to pomaga!

+0

wielkie dzięki. to działało dla mnie. Jeśli chcę wysłać coś z SepetCell do tableView, co mam zrobić? Na przykład, mam strepper i kiedy klikam na nim chcę zmienić wartość etykiety –

+2

ok Znalazłem to przez jakąś próbkę :) Wprowadziłem kilka zmian w kodzie jak: var onFavButtonTapped: ((Int) - > Void)? = Zero **** czy pozwolić onButtonTapped = self.onButtonTapped { onButtonTapped (3) } **** komórek .onButtonTapped! = {(X int) w println (x) } –

+0

wielkie słyszeć!. BTW, jeśli uważasz, że moja odpowiedź jest przydatna, proszę rozważyć awansowanie :) – raf

Powiązane problemy