简体   繁体   English

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

[英]How to increment and decrement value of label in tableview and make total price from label value in swift?

我的tableview看起来像这样。

now cell value are dynamically and its look after calling api. 现在单元格值是动态的,并且在调用api后会自动查找。 动态添加价值后,我的tableview看起来像这样。

I want to make total of all tickets price at last. 最后我想把所有门票的价格合计。 I refer this link How do I increment/decrement a label value with two buttons pressed in tableview Swift and make changes in my code but didn't work for me. 我引用此链接。 如何在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)"
  }
 }

when I increment or decrement value of first cell it reflect in 4th cell. 当我增加或减少第一个单元格的值时,它将反映在第四个单元格中。 please help. 请帮忙。 I am new at swift. 我是新手。

This is due to cell reuse. 这是由于单元重用。 You should set a model for each cell 您应该为每个单元格设置一个模型

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

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