0

I working on cart view controller I tried some of code, it's show like below image.

enter image description here

values updating every row in tableview, if I click on product1 plusbutton count is increase showing 1 .when I click on product2 plusbutton value is showing 2.count is increasing.minus every time minusbutton also working same like that.

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
         let cell = tableView.dequeueReusableCell(withIdentifier: "CustomTableViewCell") as! CustomTableViewCell
 cell.minusButton.tag = indexPath.row
        cell.plusbutton.tag = indexPath.row

        cell.minusButton.addTarget(self, action: #selector(minusbuttonClick), for: .touchUpInside)
        cell.plusbutton.addTarget(self, action: #selector(plusButtonClick), for: .touchUpInside)
        return cell
    }

    @objc func minusbuttonClick(sender : UIButton)
        {
            let cell = Tableview.cellForRow(at: NSIndexPath(row: sender.tag, section: 0) as IndexPath) as! CustomTableViewCell
            if(count > 0){
                count -= 1
            }
            let myString = String(count)
            cell.countLabel.text = myString
            if count == 0{
                 cell.countLabel.text = ""
            }
            self.Tableview.reloadData()

        }

        @objc func plusButtonClick(sender : UIButton)
        {
            let cell = Tableview.cellForRow(at: NSIndexPath(row: sender.tag, section: 0) as IndexPath) as! CustomTableViewCell
             count += 1
            let myString = String(count)
            cell.countLabel.text = myString
            self.Tableview.reloadData()
        }

I have to show when I click product1 value should be 1, if I click on product2 value as 1 minus also decrease same like that

Sandeep Bhandari
  • 19,999
  • 5
  • 45
  • 78
Dhruv
  • 13
  • 1
  • 7
  • 2
    you only have one instance of 'count' - you need an array of count to match the rows, and then use count[indexPath.row] – Russell May 20 '20 at 07:39
  • Also, cells are reused so you will end up with your action handler called multiple times for each button tap. You should handle the button tap within the cell and pass back the event using delegation or a closure https://stackoverflow.com/questions/28659845/swift-how-to-get-the-indexpath-row-when-a-button-in-a-cell-is-tapped/38941510#38941510 – Paulw11 May 20 '20 at 07:52

1 Answers1

0

first thing to do is create a struct to hold your data

struct ProductData {
    var count : Int
    var name : String
}

and you can use that in your ViewController

var productData : [ProductData] = []   

the, of course, you need to add some data - here's a simple set to get started

override func viewDidLoad() {
    super.viewDidLoad()

    // set up some data.
    productData.append(ProductData(count: 0, name: "product 1"))
    productData.append(ProductData(count: 0, name: "product 2"))
    productData.append(ProductData(count: 0, name: "product 3"))
    productData.append(ProductData(count: 0, name: "product 4"))
}

using the delegate model described earlier swift: how to get the indexpath.row when a button in a cell is tapped? update your custom table view cell

protocol TableViewCellCustomDelegate: class {
    func buttonTapped(index : Int, delta : Int)
}

class TableViewCellCustom: UITableViewCell {

    weak var delegate: TableViewCellCustomDelegate?

    @IBOutlet weak var minusButton: UIButton!
    @IBOutlet weak var plusButton: UIButton!
    @IBOutlet weak var countLabel: UILabel!
    @IBOutlet weak var productLabel: UILabel!

    override func prepareForReuse() {
        super.prepareForReuse()
        self.delegate = nil
    }

    @IBAction func minusButtonClick(_ sender: UIButton) {
        delegate?.buttonTapped(index: sender.tag, delta : -1)
    }

    @IBAction func plusButtonClick(_ sender: UIButton) {
        delegate?.buttonTapped(index: sender.tag,delta : +1)
    }
}

update the delegate methods in your view controller. I've done it here with a single method to add or delete, but you could split it out if you want to do other things as well.

extension ViewController: TableViewCellCustomDelegate {
    func buttonTapped(index: Int, delta : Int)
    {
        productData[index].count += delta
        if productData[index].count < 0
        {
            productData[index].count = 0
        }

        tableView.reloadRows(at: [IndexPath(row: index, section: 0)], with: .automatic)
    }
}

and update your cellForRowAt definition

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "CustomTableViewCell", for: indexPath) as! TableViewCellCustom
    cell.countLabel.text = "\(productData[indexPath.row].count)"
    cell.productLabel.text = productData[indexPath.row].name

    cell.minusButton.tag = indexPath.row
    cell.plusButton.tag = indexPath.row

    cell.delegate = self

    return cell
}
Russell
  • 5,436
  • 2
  • 19
  • 27