首页 > 解决方案 > 如何在tableview中增加和减少标签的值并在swift中从标签值中得出总价?

问题描述

我的 tableview 看起来像这样。

现在单元格值是动态的,它在调用 api 后的样子。 动态添加值后,我的 tableview 看起来像这样。

我想最后计算所有票价的总和。我参考了这个链接How do I increase/decrement a label value with two button press 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)"
  }
 }

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

标签: iosswiftuitableview

解决方案


这是由于细胞重用。您应该为每个单元格设置一个模型


推荐阅读