如何在按下按钮时更改徽章编号?

问题描述 投票:0回答:1

当放置计数并在单元格中按下atcbtn时,我一直在努力更改cartBtn中的徽章编号

现在CountVC在cartBtn中显示给我的是按下atcbtn时单元格的计数,但是我想做的是按下Atcbtn时获得每个单元格的总计数

例如,如果我在第一个单元格中的计数是4,而我在第二个单元格中的计数是3。 cartBtn应该在徽章中显示7,而不仅仅是徽章中一个单元格的总数如果有任何意义

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

class CountViewController: UIViewController {

    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var cartBtn: BadgeBarButtonItem!

    var productSetup: [Products] = []

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.dataSource = self
        tableView.delegate = self
    }

// changes the count on badge button but doesnt add up the count for every cell in the badege
//glitch is that it overloads the count passed into the cartVC when ATC is pressed multiple times in the cell
    @objc func cartCount(_ sender: UIButton){
        let currentCount = self.productSetup[sender.tag].count
        self.productSetup[sender.tag].count = currentCount + 1
        cartBtn.badgeNumber = currentCount
    }

    //increments the counter
    @objc func plusItem(_ sender: UIButton) {
        let currentCount = self.productSetup[sender.tag].count
        if currentCount > 9 {
            return
        }
        self.productSetup[sender.tag].count = currentCount + 1
        if let currentCell = self.tableView.cellForRow(at: IndexPath(row: sender.tag, section: 0)) as? ProductListCell {
            currentCell.lblQty.text = "\(self.productSetup[sender.tag].count)"
        }
    }

    // decrements the counter
    @objc func minusItem(_ sender: UIButton) {
        let currentCount = self.productSetup[sender.tag].count
        if !(currentCount - 1 >= 0) {
            return
        }
        self.productSetup[sender.tag].count = currentCount - 1
        if let currentCell = self.tableView.cellForRow(at: IndexPath(row: sender.tag, section: 0)) as? ProductListCell {
            currentCell.lblQty.text = "\(self.productSetup[sender.tag].count)"
        }
    }
}

extension CountViewController: UITableViewDelegate, UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return productSetup.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "CountCell") as? CountCell else { return UITableViewCell() }

        // Allows increment and decrement to work in individual cells
        cell.lblQty.text = "\(self.productSetup[indexPath.row].count)"
        cell.plusBtn.tag = indexPath.row
        cell.minusBtn.tag = indexPath.row
        cell.plusBtn.addTarget(self, action: #selector(self.plusItem(_:)), for: .touchUpInside)
        cell.minusBtn.addTarget(self, action: #selector(self.minusItem(_:)), for: .touchUpInside)


        // used to change count in cart btn
        cell.atcBtn.tag = indexPath.row
        cell.atcBtn.addTarget(self, action: #selector(self.cartCount(_:)), for: .touchUpInside)     

        return cell
    }

}

ios swift uitableview counter uibarbuttonitem
1个回答
0
投票
您需要更改代码以遍历所有产品才能得出总计:
© www.soinside.com 2019 - 2024. All rights reserved.