简体   繁体   English

Swift - 如何在按下不同自定义单元格中的按钮时获取自定义单元格标签的值?

[英]Swift - How to get the value of a custom cell label when pressing a button in a different custom cell?

Hit a bit of a brick wall with this problem. 用这个问题打了一下砖墙。

I have a TableView which of dynamic prototype. 我有一个TableView哪个动态原型。 The TableView has 2 sections. TableView有2个部分。

Section 1 loads a custom TableViewCell from a xib file. 第1节从xib文件加载自定义TableViewCell。 The cell contains a stepper and a label: 单元格包含一个步进器和一个标签:

class quantityTableViewCell: UITableViewCell {

 @IBOutlet weak var quantityLabel: UILabel!


 @IBAction func quantityStepper(_ sender: UIStepper) {
    quantityLabel.text = String(Int(sender.value))
 }

}

Section 2 loads another custom TableViewCell which contains just a button: 第2节加载另一个自定义TableViewCell,它只包含一个按钮:

class addToBasketTableViewCell: UITableViewCell {

 @IBOutlet weak var submitButton: UIButton!

}

Now in my TableView class where both cells are being loaded in their own sections, I want to capture the current value of 'quantityLabel' inside the first section when I click the button in the second section and print the result to console. 现在在我的TableView类中,两个单元格都在自己的部分中加载,我想在第二部分中单击按钮并将结果打印到控制台时,在第一部分内捕获'quantityLabel'的当前值。

For example, if I step the value to 5, when I hit the 'submitButton' it prints '5'. 例如,如果我将值调到5,当我点击'submitButton'时它会打印'5'。

I'm a bit unsure how to go about this, any guidance would be great. 我有点不确定如何解决这个问题,任何指导都会很棒。 Below is a copy of the cells being loaded: 下面是正在加载的单元格的副本:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let item: ItemPreBasketModel = cellItems[indexPath.row] as! ItemPreBasketModel

    if indexPath.section == 0 {

        let quantityCell = Bundle.main.loadNibNamed("quantityTableViewCell", owner: self, options: nil)?.first as! quantityTableViewCell
        return quantityCell

        } else if indexPath.section == 1 {

        let addToBasketCell = Bundle.main.loadNibNamed("addToBasketTableViewCell", owner: self, options: nil)?.first as! addToBasketTableViewCell
        return addToBasketCell

    }
}

It should be something like this: 它应该是这样的:

let path = IndexPath(item: 0, section: 0)
let cell = table.cellForRow(at: path) as? quantityTableViewCell
print(cell?.quantityLabel.text)

replace "table" with your table object. 用表对象替换“table”。

You should never rely on a value from a cell, because cells might appear and disappear when the user scrolls the table view. 您永远不应该依赖单元格中的值,因为当用户滚动表格视图时,单元格可能会出现和消失。

Instead, you should store the value of the stepper in a model, and when the user taps on the button, read the value from the model (and not from any cell). 相反,您应该将步进器的值存储在模型中,当用户点击按钮时,从模型中读取值(而不是从任何单元格中读取)。

So: 所以:

  • When quantityStepper is called, update the label and inform some delegate (eg the hosting view controller) that the value changed. 调用quantityStepper ,更新标签并通知某个委托(例如托管视图控制器)该值已更改。 Be aware: 意识到:
    1. You should not update the model directly from within the quantityTableViewCell 应该从内部直接更新模型quantityTableViewCell
    2. Instead, you should send a message (= your own Protocol) to some delegate (which implements this Protocol) to inform it that the value changed to some value 相反,您应该向某个委托(实现此协议)发送一条消息(=您自己的协议),以通知它该值已更改为某个值
    3. The delegate (maybe your view controller) will store this value somewhere 代理人(可能是您的视图控制器)会将此值存储在某处
  • When addToBasketTableViewCell is called, you should also inform the delegate about this. 调用addToBasketTableViewCell ,您还应该通知addToBasketTableViewCell The delegate (your view controller) then will then work with the value that he got in 3. and do whatever has to be done. 然后,委托(您的视图控制器)将使用他在3中获得的值,并执行必须执行的操作。

With this approch, the cells are decoupled from each other. 通过这个approch,细胞彼此分离。 You don't have any problems with cell reusage, and you can initialize the cells properly because the value is always stored in the model, and the cells only display it. 您对单元重用没有任何问题,并且您可以正确初始化单元格,因为该值始终存储在模型中,并且单元格仅显示它。 Updates are always reflected to the model. 更新始终反映在模型中。

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

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