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

时间:2019-12-25 21:33:43

标签: ios swift uitableview counter uibarbuttonitem

放置计数并在单元格中按下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
    }

}

1 个答案:

答案 0 :(得分:1)

您需要更改代码以遍历所有产品以得出总计:

@objc func cartCount(_ sender: UIButton){
    var totalCount = 0
    for product in productSetup {
        totalCount += product.count
    }

    cartBtn.badgeNumber = totalCount

}

您也可以单线执行此操作,但是如果您不是Swift专家,会发生什么则不太明显。对于经验不足的程序员,您是否愿意使用更少的代码还是更易于理解的代码由您决定:

@objc func cartCount(_ sender: UIButton){
    cartBtn.badgeNumber = productSetup.map({$0.count}).reduce(0,+)
}