【问题标题】:Gather textfield text from a tableview cell (Swift)从表格视图单元格中收集文本字段文本 (Swift)
【发布时间】:2018-07-07 12:51:30
【问题描述】:

我有一个表格视图,每个单元格中有一个文本字段。我添加了一个这样的目标:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "customLevelCell") as! LevelTableViewCell

    cell.cellTextField.addTarget(self, action: #selector(ViewController.TextfieldEditAction), for: .editingDidEnd)

    return cell
}

但发现我无法使用 indexpath.row / sender.tag 来获取特定的文本字段文本

@objc func TextfieldEditAction(sender: UIButton) {
}

所以我的问题是如何在用户编辑其中一个文本字段后获取文本。

另外,我如何获取 indexpath.row 或 sender.tag,它们将用于收集他们添加到特定文本字段的文本。

【问题讨论】:

  • 选择器中的函数参数在哪里
  • @RahulGUsai 更新了问题

标签: ios swift uitableview


【解决方案1】:

处理这个问题的最简单方法可能是使用委托协议……

在你的牢房里

protocol LevelTableViewCellDelegate: class {
    func levelTableViewCell(_ levelTableViewCell: LevelTableViewCell, didEndEditingWithText: String?)
}

class LevelTableViewCell: UITableViewCell {
    @IBOutlet private weak var cellTextField: UITextField!
    var delegate: LevelTableViewCellDelegate?

    override func awakeFromNib() {
        cellTextField.addTarget(self, action: #selector(didEndEditing(_:)), for: .editingDidEnd)
    }

    @objc func didEndEditing(_ sender: UITextField) {
        delegate?.levelTableViewCell(self, didEndEditingWithText: sender.text)
    }
}    

在您的视图控制器中

class TableViewController: UITableViewController {

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "LevelTableViewCell") as! LevelTableViewCell
        cell.delegate = self
        return cell
    }
}

extension TableViewController: LevelTableViewCellDelegate {

    func levelTableViewCell(_ levelTableViewCell: LevelTableViewCell, didEndEditingWithText: String?) {

        let indexPath = tableView.indexPath(for: levelTableViewCell)

        // Now you have the cell, indexPath AND the string
    }

另外,请注意查看出口是private。如果你遵循这个规则,你会发现你写的代码更干净

【讨论】:

  • 可以省略第二个参数,使用levelTableViewCell.cellTextField.text
  • 你可以,但我不会。
  • 真的,不要那样做。正如我在回答中指出的那样,保留网点private 将改进您的代码。
  • 如果您将cellTextField 出口设为私有,那么您需要提供一些适当的方法来设置来自cellForRowAt 的单元格文本。就个人而言,我会将文本字段出口设为公开,但将其设为只读。很像UITableViewCelltextLabel。这样,可以从cellForRowAt 配置文本字段(提示、文本、字体等)。或者保持插座私有并添加其他方式来设置所有这些属性。
  • 上面的代码不是一个完整的例子——正如你所说,我没有包含配置文本字段的方法。但是保持网点私有会迫使单元进行自我配置,并且(正确地,在我看来)从视图控制器中移除了这个责任。尽管基本的UITableViewCelltextLabelpublic,但从视图控制器更新其属性还是有点味道。
【解决方案2】:

以下是UIView的扩展,可以用来获取单元格或者包围textField的单元格的indexPath

extension UIView {

    var tableViewCell : UITableViewCell? {

        var subviewClass = self

        while !(subviewClass is UITableViewCell){

            guard let view = subviewClass.superview else { return nil }

            subviewClass = view
        }
        return subviewClass as? UITableViewCell
    }

    func tableViewIndexPath(_ tableView: UITableView) -> IndexPath? {

        if let cell = self.tableViewCell {

            return tableView.indexPath(for: cell)

        }
        return nil
    }
}

示例:-

    @objc func TextfieldEditAction(sender: UITextField) {

        //replace tableView with the name of your tableView
        guard let indexPath = sender.tableViewIndexPath(tableView) else {return}

    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-18
    • 1970-01-01
    • 2015-08-29
    相关资源
    最近更新 更多