如何从动态表格视图单元格快速按钮单击获得文本字段值

问题描述 投票:0回答:1

因此,我正在使用dequeueReusableCell,并且将显示几个具有不同行号的“当前价格”和“更改价格”的表视图,具体取决于我选择的产品。如何从Label change 1、2、3等中获取文本字段的值?由于它是动态原型,因此我不知道单击Change Price按钮时如何获取文本字段值。

我有这个课,他们连接插座

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!

}

而且我的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
    }

这是我的功能,可以将价格更改为我被困的Firebase:

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

        }
    }

我被卡住了,因为我不知道如何获得像这样的动态单元格的值。任何帮助将不胜感激。

谢谢

enter image description here

ios swift firebase tableview swift5
1个回答
1
投票

您需要从cellForRow中获取UITableView并将其转换为您的UITableViewCell类,即priceListCell,然后可以从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)
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.