简体   繁体   English

如何从动态表格视图单元格 swift 获取按钮单击时的文本字段值

[英]How to get textfield value on button click from dynamic table view cell swift

So, I'm using dequeueReusableCell and will show several Table View with different row numbers of Current Price and and Change Price, depending on the product that I choose.因此,我使用dequeueReusableCell并将显示几个表视图,其中包含当前价格和更改价格的不同行数,具体取决于我选择的产品。 How do I get the value of the text field from Label change 1, 2, 3 etc?如何从 Label 更改 1、2、3 等获取文本字段的值? Since it is a dynamic prototype, I do not know how to get the text field value when the Change Price button is clicked.由于是动态原型,所以不知道点击Change Price按钮时如何获取文本字段值。

I have this class the connects the outlets我有这个 class 连接插座

class priceListCell: UITableViewCell {
    // CURRENT PRICE
    @IBOutlet weak var lblProductNameCurrentPrice: UILabel!
    @IBOutlet weak var txtViewCurrentPrice: UITextView!

    // EDIT PRICE
    @IBOutlet weak var lblProductNameEditPrice: UILabel!
    @IBOutlet weak var txtFieldEditPrice: UITextField!
    @IBOutlet weak var btnChangePrice: UIButton!

}

And my TableView code is like this:我的 TableView 代码是这样的:

....
    case 1:
       if let editPriceCell = tableView.dequeueReusableCell(withIdentifier: "editPriceCell", for: indexPath) as? priceListCell {
       let product = editProducts[indexPath.row]
       editPriceCell.lblProductNameEditPrice.text = product.productName
       return editPriceCell
    }
    case 2:
       if let changePriceCell = tableView.dequeueReusableCell(withIdentifier: "changePriceCell", for: indexPath) as? priceListCell {
       return changePriceCell
    }

And here's my function to change price to firebase where i'm stuck at:这是我的 function 将价格更改为 firebase 我被困在的地方:

func changeValue() {
        for rowIndex in 0...self.tableView.numberOfRows(inSection: 3) {
            let indexPath = NSIndexPath(item: rowIndex, section: 3)

        }
    }

i'm stuck cause i don't know how to get the value of dynamic cell like this.我被卡住了,因为我不知道如何获得这样的动态单元格的值。 Any help will be appreciated.任何帮助将不胜感激。

Thankyou谢谢

在此处输入图像描述

You need to get the cellForRow from UITableView and cast it as your class of UITableViewCell ie., priceListCell and then you can get your value from UITableViewCell , here's how:您需要从UITableView获取cellForRow并将其转换为您的 class 的UITableViewCellpriceListCell ,然后您可以从UITableViewCell获得您的价值,方法如下:

func changeValue() {
    for rowIndex in 0..<tableView.numberOfRows(inSection: 3) {
        let indexPath = IndexPath(row: rowIndex, section: 3)
        if let editPriceCell = tableView.cellForRow(at: indexPath) as? priceListCell {
            print(editPriceCell.lblProductNameEditPrice.text)
        }
    }
}

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

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