- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 UITableViewController,它有两个独特的自定义 UITableViewCells。独特的单元格是一个 HeaderCell
(第 0 行),它包含名为 orderPriceLabel
的 UILabel 中的整个订单的价格,以及 ItemCell
(第 1 行+) 它有一个 itemPriceLabel
,它在其索引路径中存储每个项目的价格,还有一个 countLabel
,它存储每个项目被添加到订单中的次数.
每个 ItemCell
都有一个 +
UIButton 和一个 -
UIButton。每次点击+
按钮,对应索引路径的countLabel
增加,如果点击-
按钮,则 countLabel
减少。这部分按我希望的方式工作。我遇到的困难是更新第 0 行 HeaderCell
中的 orderPriceLabel
。点击 +
按钮时,orderPriceLabel
应该增加存储在 ItemCell
的 itemPriceLabel
中的值,而当点击 -
按钮时会发生相反的情况。
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
这将创建一个可以在整个类(class)中轻松访问的变量。当您单击 ItemCell
中的 + 或 – 按钮时,将必要的值添加到 totalPrice
中,其中 itemPriceValue
是您要添加的内容:
self.totalPrice += itemPriceValue
现在,您可以使 orderPriceLabel
等于 totalPrice
的值。此外,为确保 UITableView 更新,请将此方法添加到 TableViewController 代码的底部:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.tableView.reloadData()
}
关于Swift:如何在同一个 UITableViewController 中的唯一 UITableViewCells 之间传递数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39989089/
我是一名优秀的程序员,十分优秀!