【问题标题】:How to get textfield value on button click from dynamic table view cell swift如何从动态表格视图单元格中快速获取按钮单击时的文本字段值
【发布时间】:2020-10-01 20:34:50
【问题描述】:

所以,我使用dequeueReusableCell 并将显示几个表视图,其中包含当前价格和更改价格的不同行号,具体取决于我选择的产品。 如何从标签更改 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
    }

这是我的功能,可以将价格更改为我遇到的火力基地:

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

        }
    }

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

谢谢你

【问题讨论】:

    标签: ios swift firebase tableview swift5


    【解决方案1】:

    您需要从UITableView 获取cellForRow 并将其转换为您的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)
            }
        }
    }
    

    【讨论】:

    • 为什么会出现这个错误? Exception: "Requested the number of rows for section (4) which is out of bounds."
    • 部分的数量是多少?
    • @AldoSugiarto 你一定要给0...tableView.numberOfRows 你确定要给0..&lt;tableView.numberOfRows 清楚地检查我答案的第二行。
    • 我有一个错误,节数错误。它应该是 (2) 文本字段在第 2 节中的位置。我检查了0...tableView.numberOfRows。它现在有这样的错误:Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value
    • 这是我最后的代码:func changeValue() { for rowIndex in 0...tableView.numberOfRows(inSection: 2) { let indexPath = IndexPath(item: rowIndex, section: 2) if let editPriceCell = self.tableView.cellForRow(at: indexPath) as? priceListCell { print(editPriceCell.txtFieldEditPrice.text) } } }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-18
    • 2021-01-15
    相关资源
    最近更新 更多