如何从动态 table 视图单元格 swift 单击按钮时获取文本字段值
How to get textfield value on button click from dynamic table view cell swift
因此,我正在使用 dequeueReusableCell
并将显示多个 Table 视图,其中包含当前价格和更改价格的不同行号,具体取决于我选择的产品。
如何从标签更改 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!
}
而我的Table查看代码是这样的:
....
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)
}
}
我卡住了,因为我不知道如何像这样获取动态单元格的值。
任何帮助将不胜感激。
谢谢
您需要从 UITableView
获取 cellForRow
并将其转换为 UITableViewCell
的 class 即 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)
}
}
}
因此,我正在使用 dequeueReusableCell
并将显示多个 Table 视图,其中包含当前价格和更改价格的不同行号,具体取决于我选择的产品。
如何从标签更改 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!
}
而我的Table查看代码是这样的:
....
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)
}
}
我卡住了,因为我不知道如何像这样获取动态单元格的值。 任何帮助将不胜感激。
谢谢
您需要从 UITableView
获取 cellForRow
并将其转换为 UITableViewCell
的 class 即 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)
}
}
}