title: How to increment and decrement variable in Tableview cell in IOS Swift4
body: <p>I have a data in tableview. I have two buttons + and -, i want to increase and decrease value on click of buttons. please check below image to understand exactly.</p>
  
  <p><a href="https://i.stack.imgur.com/mQdoX.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/mQdoX.png" alt="enter image description here"></a></p>
  
  <p>When i press on first cell's plus icon, the value is reflecting in last cell.</p>
  
  <p>This is my code :- </p>
  
  <pre><code>import UIKit
  struct Product {
   var price = 0
  }
  
  class TicketBookingVC: UIViewController , UITableViewDelegate,       UITableViewDataSource {
  
  @IBOutlet weak var tblView: UITableView!
  @IBOutlet weak var mainTblView: UIView!
  
  var bookingDetails = NSDictionary()
  var paymentDetails = NSDictionary()
  var arrPaymentDetails = NSArray()
  var productArray = [Product]()
  var product : Product!
  private var counterValue = 1
  var productIndex = 0
  var counterLbl = UILabel()
  
  @IBOutlet weak var bookBtn: UIButton!
  @IBOutlet weak var eventImg: UIImageView!
  
  
  override func viewDidLoad() {
      super.viewDidLoad()
  
      tblView.delegate = self
      tblView.dataSource = self
  
  
      let payment = self.paymentDetails.value(forKey: "payment") as! NSArray
      self.arrPaymentDetails = payment as NSArray
  
      for _ in 0...10{
          productArray.append(Product(price: 1))
      }
  
      // Do any additional setup after loading the view.
  }
  
  func numberOfSections(in tableView: UITableView) -&gt; Int {
  
      return 3
  }
  
  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
  
      if section == 0 {
          return 1
      }
      else if section == 1{
          return arrPaymentDetails.count
      }
      else{
          return 1
      }
  }
  
  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {
  
      if indexPath.section == 0 {
  
          let cell = tableView.dequeueReusableCell(withIdentifier: "cellfirst", for: indexPath)
  
  
          cell.selectionStyle = .none
          return cell
      }
    else if indexPath.section == 1 {
  
         let cell = tableView.dequeueReusableCell(withIdentifier: "cellsecond", for: indexPath)
  
          let mainViewCell = cell.contentView.viewWithTag(2000) as! UIView
          let normalView = cell.contentView.viewWithTag(2001) as! UIView
          let eventName = cell.contentView.viewWithTag(2003) as! UILabel
          let eventPrice = cell.contentView.viewWithTag(2004) as! UILabel
          counterLbl = cell.contentView.viewWithTag(2007) as! UILabel
          let decrementBtn = cell.contentView.viewWithTag(2005) as! UIButton
          let incrementBtn = cell.contentView.viewWithTag(2006) as! UIButton
  
          let dictAllDetails = self.arrPaymentDetails.object(at: indexPath.row) as! NSDictionary
          print("dictallgroups : \(dictAllDetails)")
  
          if dictAllDetails.value(forKey: "label") != nil {
  
              eventName.text = dictAllDetails.value(forKey: "label") as! String
          }
          else{
              eventName.text = ""
          }
  
          if dictAllDetails.value(forKey: "price") != nil {
  
              eventPrice.text = "₹ \(dictAllDetails.value(forKey: "price") as! String)"
          }
          else{
              eventPrice.text = "₹ 0"
          }
  
          decrementBtn.addTarget(self, action:#selector(self.decrementbuttonClicked), for: .touchUpInside)
          incrementBtn.addTarget(self, action:#selector(self.incrementbuttonClicked), for: .touchUpInside)
  
          product = productArray[indexPath.row]
          counterLbl.text = "\(product.price)"
          productIndex = indexPath.row
  
         cell.selectionStyle = .none
         return cell
    }
      else {
          let cell = tableView.dequeueReusableCell(withIdentifier: "cellthird", for: indexPath)
  
  
          cell.selectionStyle = .none
          return cell
      }
  }
  
  func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -&gt; CGFloat {
  
      if indexPath.section == 0{
  
          return UITableView.automaticDimension
      }
      else{
          return 80
          //return UITableView.automaticDimension
      }
  }
  
  @objc func decrementbuttonClicked() {
      print("Button decrement")
      if(counterValue != 1){
          counterValue -= 1;
      }
      print("\(counterValue)")
      self.counterLbl.text = "\(counterValue)"
      product.price = counterValue
      print("\(product.price)")
      addProductToCart(product: product, atindex: productIndex)
  }
  
  @objc func incrementbuttonClicked() {
      print("Button increment")
      counterValue += 1;
      print("\(counterValue)")
      self.counterLbl.text = "\(counterValue)"
      product.price = counterValue
      print("\(product.price)")
      addProductToCart(product: product, atindex: productIndex)
  }
  
  func addProductToCart(product: Product, atindex: Int) {
      productArray[atindex] = product
  
      calculateTotal()
  }
  
  func calculateTotal()
  {
      var totalValue = 0
      for objProduct in productArray {
  
          totalValue += objProduct.price
      }
  
     // self.totalLabel.text = "Total \(totalValue)"
  }
  </code></pre>
  
  <p>}</p>
  
  <p>I tried this link also :-</p>
  
  <p><a href="https://stackoverflow.com/questions/53996781/quantity-of-products-in-cart-view-controller-with-buttons-each-on-a-tablevie">+/- quantity of products in Cart View Controller with buttons each on a TableViewCell, which gets default quantity as integer from an array</a></p>
  
  <p><a href="https://stackoverflow.com/questions/48299611/how-do-i-increment-decrement-a-label-value-with-two-buttons-pressed-in-tableview">How do I increment/decrement a label value with two buttons pressed in tableview Swift</a></p>
  
  <p>Thank you in advance.</p>