如何在表视图中递增和递减标签值并快速从标签值中得出总价?

时间:2019-07-08 07:26:25

标签: ios swift uitableview

My tableview look like this.

现在单元格值是动态的,并且在调用api后会自动查找。   my tableview look like this after dynamically add value.

我想最后把所有票的价格合计。我引用了此链接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)"
  }
 }

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

1 个答案:

答案 0 :(得分:1)

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