Swift:如何在同一UITableViewController中的唯一UITableViewCell之间传递数据

Swift:如何在同一UITableViewController中的唯一UITableViewCell之间传递数据,swift,uitableview,Swift,Uitableview,我有一个UITableViewController,它有两个唯一的自定义UITableViewCell。唯一的单元格是一个HeaderCell(第0行),它在一个名为orderpricelab的UILabel和ItemCell(第1行+)中包含整个订单的价格它有一个itemPriceLabel,用于在索引路径中存储每个项目的价格,还有一个countLabel,用于存储每个项目添加到订单中的次数 每个ItemCell都有一个+ui按钮和一个-ui按钮。每次点击+按钮时,相应索引路径上的countL

我有一个UITableViewController,它有两个唯一的自定义UITableViewCell。唯一的单元格是一个
HeaderCell
(第0行),它在一个名为
orderpricelab
的UILabel和
ItemCell
(第1行+)中包含整个订单的价格它有一个
itemPriceLabel
,用于在索引路径中存储每个项目的价格,还有一个
countLabel
,用于存储每个项目添加到订单中的次数

每个
ItemCell
都有一个
+
ui按钮和一个
-
ui按钮。每次点击
+
按钮时,相应索引路径上的
countLabel
会增加,如果点击
-
按钮,则
countLabel
会减少。这部分按我所希望的方式工作。我正在努力更新第0行
HeaderCell
中的
orderpricelab
。点击
+
按钮时,
订单价格标签
应增加
项目单元格
项目价格标签
中存储的值,点击
-
按钮时应相反

ItemCell代码:

class ItemCell: UITableViewCell {

// MARK: Properties
@IBOutlet weak var itemOrderCountLabel: UILabel!
@IBOutlet weak var itemPriceLabel: UILabel!

var count: Int = 0

override func awakeFromNib() {
    super.awakeFromNib()

}

override func setSelected(selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)


}

@IBAction func decrementButton(sender: AnyObject) {
    count -= 1
    itemOrderCountLabel.text = "\(count)"
    //How to decrease orderPriceLabel

}

@IBAction func incrementButton(sender: AnyObject) {
    count += 1
    itemOrderCountLabel.text = "\(count)"
    //How to increase orderPriceLabel

}
HeaderCell代码:

class FoodMenuTableViewCell: UITableViewCell {

// MARK: Properties
@IBOutlet weak var orderPriceLabel: UILabel!

override func awakeFromNib() {
    super.awakeFromNib()

}

override func setSelected(selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)


}
TableViewController代码:

class FoodMenuTableVC: UITableViewController {

override func viewDidLoad() {
    super.viewDidLoad()

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

}

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {

    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    itemList.count + 1

}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    if indexPath.row == 0 {

        let cellIdentifier = "headerCell"

        let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! HeaderCell

        //cell.orderPriceLabel.text = ....?

        return cell

    } else {

        //This works fine

        let cellIdentifier = "itemCell"

        let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! ItemCell

        let name = localSelectedVendorName!


            let keys = Array(itemList.keys)
            let values = Array(itemList.values)

            cell.itemPriceLabel.text = String(values[indexPath.row - 1].price)
            cell.itemOrderCountLabel.text = " "

        }

        return cell
    }
总的来说,我对swift和编程很在行


谢谢你的帮助

我建议将总价存储在类变量(属性)中。在TableViewController类中的
viewDidLoad
上方添加这行代码:

var totalPrice = 0
这将创建一个可以在整个类中轻松访问的变量。单击
ItemCell
中的+或–按钮时,向
totalPrice
添加必要的值,如下所示,
itemPriceValue
就是您要添加的内容:

self.totalPrice += itemPriceValue
现在,您可以使您的
orderpricelab
等于
totalPrice
的值。此外,要确保UITableView更新,请将此方法添加到TableViewController代码的底部:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    self.tableView.reloadData()
}

我建议将总价存储在类变量(属性)中。在TableViewController类中的
viewDidLoad
上方添加这行代码:

var totalPrice = 0
这将创建一个可以在整个类中轻松访问的变量。单击
ItemCell
中的+或–按钮时,向
totalPrice
添加必要的值,如下所示,
itemPriceValue
就是您要添加的内容:

self.totalPrice += itemPriceValue
现在,您可以使您的
orderpricelab
等于
totalPrice
的值。此外,要确保UITableView更新,请将此方法添加到TableViewController代码的底部:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    self.tableView.reloadData()
}

我认为这不起作用,因为按钮存储在ItemCell类中。目前我使用的是一个可以正确更新的全局变量,但它在HeaderCell中没有更新。我认为这不起作用,因为按钮存储在ItemCell类中。目前,我正在使用一个可以正确更新的全局变量,但它在HeaderCell中没有更新。