ios - 如何在 tableview 中增加和减少标签的值,并在 swift 中根据标签值计算总价?

标签 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)"
  }
 }

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

最佳答案

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

关于ios - 如何在 tableview 中增加和减少标签的值,并在 swift 中根据标签值计算总价?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56930158/

相关文章:

ios - 如何在 swift 5 中的每个 Controller 上调用一个函数

ios - 为静态 TableView 单元格设置复选标记

iphone - 如何在 iPhone 中的部分 tableviewcell 中添加行

ios - 如何在 UITableView 中找到完全可见(完全可见)单元格的索引

ios - 连接到 Wi-Fi 网络时,获取其所有接入点

iphone - UITableView Cell - 从按钮获取 IndexPath.row?

ios - 何时以及何时不在 Objective-C 中使用 __block?

iphone - 将 MKCircle 半径转换为 ​​CoreGraphics 圆弧半径

ios - Google Plus 按钮在 Swift iOS 中不起作用

ios - 查找 iOS URL Schemes 链接