On my main page, I created an xib file for a UITableViewCell. I load a cell from this xib file and its performance.
Inside the cell, I have several labels and buttons. I am trying to change a shortcut by clicking on a button in a cell.
My code is like below
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()
These are my xib files for codes like .swift.
The codes on the main page will like it below:
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) { } }
When I clicked the fav button inside the cell. I would like to change the label text to anything, for example.
When I clicked the fav button, the event will go to the SepetCell.swift favoriteClicked function (sender: UIButton).
So, if I try to call: SepetViewController (). Favorite (sender.tag)
He will go inside
func favorite(tag: Int) { sepetTable.reloadData() }
but sepetTable is zero when it is gone. I think this is because I call this function SepetViewController (). Favorite (sender.tag). First, it creates the SepetViewController class. Therefore, due to the fact that the object is not installed, it gets null.
How can I contact this sepetTable or how best to solve this problem.
Thanks.