繁体   English   中英

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

[英]How to change badge number when button is pressed?

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

现在,CountVC 在cartBtn 中向我显示的是按下atcbtn 时单元格的计数,但我想做的是在按下Atc btn 时获取每个单元格的总计数

比如说,如果我在第一个单元格中的计数为 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
    }

}

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

@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,+)
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM