ios - 如何在表 View 中递增和递减标签值并快速从标签值中得出总价?-LMLPHP
现在,单元值是动态的,并且它在调用api之后进行查找。
ios - 如何在表 View 中递增和递减标签值并快速从标签值中得出总价?-LMLPHP
我想最后把所有的票价合计一下。我引用这个链接How do I increment/decrement a label value with two buttons pressed in tableview Swift并对我的代码进行更改,但对我不起作用。

 struct Product {
     var price = 0
  }

class TicketBookingVC: UIViewController , UITableViewDelegate, UITableViewDataSource {

@IBOutlet weak var tblView: UITableView!
@IBOutlet weak var mainTblView: UIView!

var bookingDetails = NSDictionary()
var productArray = [Product]()
var product : Product!
private var counterValue = 1
var productIndex = 0
var counterLbl = UILabel()

@IBOutlet weak var bookBtn: UIButton!
@IBOutlet weak var eventImg: UIImageView!

override func viewDidLoad() {
    super.viewDidLoad()

    tblView.delegate = self
    tblView.dataSource = self

        for _ in 0...10{
            productArray.append(Product(price: 1))
        }
    }
}

func numberOfSections(in tableView: UITableView) -> Int {
    return 3
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if section == 0 {
        return 1
    }
    else if section == 1{
        return 4
    }
    else{
        return 1
    }
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    if indexPath.section == 0 {

        let cell = tableView.dequeueReusableCell(withIdentifier: "cellfirst", for: indexPath)

        cell.selectionStyle = .none
        return cell
    }
  else if indexPath.section == 1 {

       let cell = tableView.dequeueReusableCell(withIdentifier: "cellsecond", for: indexPath)

        let mainViewCell = cell.contentView.viewWithTag(2000) as! UIView
        let normalView = cell.contentView.viewWithTag(2001) as! UIView
        let eventName = cell.contentView.viewWithTag(2003) as! UILabel
        let eventPrice = cell.contentView.viewWithTag(2004) as! UILabel
        counterLbl = cell.contentView.viewWithTag(2007) as! UILabel
        let decrementBtn = cell.contentView.viewWithTag(2005) as! UIButton
        let incrementBtn = cell.contentView.viewWithTag(2006) as! UIButton

        decrementBtn.addTarget(self, action:#selector(self.decrementbuttonClicked), for: .touchUpInside)
        incrementBtn.addTarget(self, action:#selector(self.incrementbuttonClicked), for: .touchUpInside)

        product = productArray[indexPath.row]
        counterLbl.text = "\(product.price)"

       cell.selectionStyle = .none
       return cell
  }
    else {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cellthird", for: indexPath)

        cell.selectionStyle = .none
        return cell
    }
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

    if indexPath.section == 0{

        return UITableView.automaticDimension
    }
    else{
        return 80
        //return UITableView.automaticDimension
    }
}

@objc func decrementbuttonClicked() {
    print("Button decrement")
    if(counterValue != 1){
        counterValue -= 1;
    }
    self.counterLbl.text = "\(counterValue)"
    product.price = counterValue
}

@objc func incrementbuttonClicked() {
    counterValue += 1;
    self.counterLbl.text = "\(counterValue)"
    product.price = counterValue
}

func addProductToCart(product: Product, atindex: Int) {
    productArray[atindex] = product
    calculateTotal()
}

func calculateTotal()
{
    var totalValue = 0
    for objProduct in productArray {

        totalValue += objProduct.price
    }
    self.eventPrice.text = "Total \(totalValue)"
  }
 }

当我增加或减少第一个单元格的值时,它反映在第四个单元格中。请帮忙。我是斯威夫特的新手。

最佳答案

这是由于单元重用。你应该为每个单元设置一个模型

10-06 09:20