0

I want to change label text when i was click button in every TableViewCell

I mean have 2(-,+) button and 1 label, When I click on the button, the label will increase or decrease.

I have no idea How to do it? sample image

RajeshKumar R
  • 15,445
  • 2
  • 38
  • 70
Tolga İskender
  • 158
  • 2
  • 14

2 Answers2

2

you need to maintain a quantityArray to store current quantity of every index

var quantityArray:[Int] = [] //initialize quantity array

then add initial quantity values to quantityArray For example : If your list array count 10 and initial quantity of all item will be one means add initial values to the array

for i in 0 ..< 10 {
   quantityArray.append(1)
}

then in your tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)

assign quantity value from quantityArray

cell.qtyLbl.text = "\(quantityArray[indexPath.row])"
cell.incrementBtn.tag = indexPath.row
cell.incrementBtn.addTarget(self, action: #selector(self.incrementBtnClicked(_:)), for: .touchUpInside)
cell.decrementBtn.tag = indexPath.row
cell.decrementBtn.addTarget(self, action: #selector(self.decrementBtnClicked(_:)), for: .touchUpInside)

add below functions into your viewcontroller

func incrementBtnClicked(_ sender:UIButton){
    let increasedQty = quantityArray[sender.tag]+1
    self.quantityArray.replaceSubrange(sender.tag, with: increasedQty)
    self.tableView.reloadData
}

func decrementBtnClicked(_ sender:UIButton){
    let decreasedQty = quantityArray[sender.tag]-1
    self.quantityArray.replaceSubrange(sender.tag, with: decreasedQty)
    self.tableView.reloadData
}

If you use like this even after scroll tableview also quantity in the cell will be populated from quantity array

Hope this will help you

Ganesh Manickam
  • 2,113
  • 3
  • 20
  • 28
  • let increasedQty = quantityArray[indexPath.row]-1 i getting error at indexpath.row – Tolga İskender Nov 28 '17 at 12:17
  • i change functions this way and worked let azalt = Int(urunler.adet[sender.tag])!-1 self.urunler.adet[sender.tag] = String(azalt) self.sepetimTableview.reloadData() . thanks – Tolga İskender Nov 28 '17 at 12:30
  • @GaneshManickam its giving an error on sender.tag saying "expected Range found Int" – Neck Jun 15 '18 at 04:28
  • have you set the tag value for buttons? @Neck – Ganesh Manickam Jun 15 '18 at 04:54
  • @GaneshManickam Yes I have tagged them as you mentioned here. like: "itemCell.itemIncrementButton.tag = indexPath.section" and even tried with: "itemCell.itemIncrementButton.tag = indexPath.row" – Neck Jun 15 '18 at 04:57
  • Can you post that as separate question and tag my name in comment? @Neck – Ganesh Manickam Jun 15 '18 at 05:22
  • @GaneshManickam here is my question [https://stackoverflow.com/questions/50869215/how-to-update-uilabel-on-a-button-click-in-uitableviewcell-in-swift-4-and-xcode/50869263#50869263] – Neck Jun 15 '18 at 05:24
0

All you have to do is to add an action to your button and do your job.

@IBAction func increase() {
    quantityLabel.text = "\(quantity)" // increase the quantity number and set it
}

btw. before getting any help you should consider posting your code.

marshallino16
  • 2,645
  • 15
  • 29