2

I have a label and tableview in a viewcontroller as it shown below:

enter image description here

I want when user taps stepper button on tableViewCell then upper label ( the number below the green button) gets updated. Yet I can't access label from my cell's class.

Any suggestion? EDIT: This is my tablevc code:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "basket", for: indexPath) as! basketcell2
    cell.ConfigureCell(number: basket[basketfoodtitle[indexPath.row]]!, price: foodtitleprice[basketfoodtitle[indexPath.row]]!, foodtitle: basketfoodtitle[indexPath.row])

    return cell
}

and this is my cell code:

@IBAction func steperchanged(_ sender: Any) {
    NumberOfFood.text = String(Int(stepper.value))
    let totalprice = Int(stepper.value)*foodtitleprice[foodtitle.text!]!
    TotalPrice.text = totalprice.stringFormatedWithSepator

}
func ConfigureCell(number:Int,price:Int,foodtitle:String)  {
        stepper.value = Double(number)
        Price.text = price.stringFormatedWithSepator
        NumberOfFood.text = String(number)
        self.foodtitle.text = foodtitle
        let totalprice = Int(stepper.value) * foodtitleprice[foodtitle]!
        TotalPrice.text = totalprice.stringFormatedWithSepator
    }
  • 2
    options are: delegates, static method of VC, Notification center, etc – RinaLiu Mar 02 '17 at 15:55
  • This answer can help you: http://stackoverflow.com/questions/41363854/get-reference-from-tableviewcells-in-tableview-ios/41364405#41364405 – Adagio Mar 02 '17 at 16:03
  • you just made me feel hungry for pizza :D – mfaani Mar 02 '17 at 16:40
  • About what RinaLui has said: I recommend you see [this](http://stackoverflow.com/a/31934786/5175709) answer. Ultimately you must learn how to use delegates. Try to apply that answer and then show some code, if it didn't work out for you then, paste **your code** and get further help – mfaani Mar 02 '17 at 16:44
  • @Honey I saw the delegate solution but it's for passing data between vc's mine is from one vc :( can you please wrie delegate solution for this case? –  Mar 02 '17 at 22:03
  • @M_STRM74 it's irrelevant. delegates are a core design pattern used between 2 classes. Sometimes it's 2 viewControllers, Sometimes it's between a Networkhandling class and another class(model, VC). Can you first try writing your code, then paste it here, even if your unsuccessful? Another very good tutorial to see is this [link](https://useyourloaf.com/blog/quick-guide-to-swift-delegates/) – mfaani Mar 02 '17 at 22:09
  • Have u used tableViews? Don't you ever do `tableView.delegate = self`. It's a similar concept...Also see [this](http://stackoverflow.com/a/39457213/5175709). It'll help you figure out what you don't know – mfaani Mar 02 '17 at 22:20
  • @Honey yes this is tableviews and all of cell's prices must sum and result is shown on top label , even when user uses stepper in each cell toplabel must update –  Mar 02 '17 at 22:25

1 Answers1

0

the best way to go forward is through using delegates but still if thats not possible please check the below code this is the easiest

var counterForTopLabel : Int = Int(){
  didSet{
    self.counterLabel.text = "\(counterForTopLabel)";
  }
}

and in your cellForRowAtIndexPath you can add the targets for buttons

cell.incrementCounterButton.addTarget(self, action: #selector(self.tappedIncrementButton(sender:)), for: .touchUpInside);
cell.decrementCounterButton.addTarget(self, action: #selector(self.tappedDecrementButton(sender:)), for: .touchUpInside);

then these functions will be declared below

func tappedIncrementButton(sender : UIButton){
    counterForTopLabel = counterForTopLabel + 1;
}
func tappedDecrementButton(sender : UIButton){
    counterForTopLabel = counterForTopLabel - 1;
}
Moin
  • 121
  • 3