簡體   English   中英

如何從動態表格視圖單元格 swift 獲取按鈕單擊時的文本字段值

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

因此,我使用dequeueReusableCell並將顯示幾個表視圖,其中包含當前價格和更改價格的不同行數,具體取決於我選擇的產品。 如何從 Label 更改 1、2、3 等獲取文本字段的值? 由於是動態原型,所以不知道點擊Change Price按鈕時如何獲取文本字段值。

我有這個 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!

}

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

這是我的 function 將價格更改為 firebase 我被困在的地方:

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

        }
    }

我被卡住了,因為我不知道如何獲得這樣的動態單元格的值。 任何幫助將不勝感激。

謝謝

在此處輸入圖像描述

您需要從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