[英]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 的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)
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.