按下按钮时如何更改徽章编号?
How to change badge number when button is pressed?
当放置计数并在单元格中按下 atcbtn 时,我一直在努力更改 cartBtn 中的徽章编号
现在 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({[=11=].count}).reduce(0,+)
}
当放置计数并在单元格中按下 atcbtn 时,我一直在努力更改 cartBtn 中的徽章编号
现在 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({[=11=].count}).reduce(0,+)
}