0

I have button in cell and I'm using protocols and delegates.

I get Quantity in product from api I add this Quantity in textfield. I want to Increase and Decrease same Quantity I get from api

I get Quantity here

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "CartCell") as! CartTableViewCell
    
    let Quantity = self.items[indexPath.row]["quantity"]
    cell.ItemsQuantityText.text = Quantity as? String

code Protocol in tableviewcell

protocol PlusOrNegative {

    func PlusandNegative (index: Int, counts: String)

}

code Button in tableviwecell

@IBAction func PlusItemsButton(_ sender: Any) {



    
    cellPlusOrNegative?.PlusandNegative(index: index!.row, counts: "")

}

code in Viewcontroller

extension CartViewController: PlusOrNegative {

func PlusandNegative(index: Int, counts: String) {
    
    let productid = items[index]["product_id"]
    self.productId = productid as! String
    print(productid!)
    
    
    let product_miniWeight = items[index]["product_miniWeight"]
    self.quantity = product_miniWeight as! String
    print(product_miniWeight!)

}
aheze
  • 24,434
  • 8
  • 68
  • 125
  • you shouldn't share images but code instead. can you edit your question please – Mat Feb 22 '21 at 19:01
  • Your cell shouldn't need to know its row and the delegate method shouldn't pass the row or the quantity. Your delegate should call back to the view controller (via delegation or a closure) and simply let it know that the button was tapped. The view controller can then perform the update to quantity and reload or update the cell. See [here](https://stackoverflow.com/questions/28659845/how-to-get-the-indexpath-row-when-an-element-is-activated/38941510#38941510) for some approaches on handling the button tap in a cell – Paulw11 Feb 22 '21 at 20:07

0 Answers0