【问题标题】:Get and pass text from a TextField which is in a custom TableViewCell subclass从自定义 TableViewCell 子类中的 TextField 获取并传递文本
【发布时间】:2020-03-24 14:19:21
【问题描述】:

如何使用自定义单元格中的文本字段中的文本?

这是接收控制器:

class ShowName: UIViewController {


    @IBOutlet weak var showName: UILabel!


    @IBAction func unwindToShowNameData(_ unwindSegue: UIStoryboardSegue) {
        let sourceViewController = unwindSegue.source as! enterName
            showName.text = sourceViewController.name
    }


    @IBAction func unwindToShowName(_ unwindSegue: UIStoryboardSegue) {
    }

}

这是发送控制器:

class enterName: UIViewController, UITableViewDataSource, UITableViewDelegate {

    var name: String?

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }

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



    @IBAction func clickSave(_ sender: UIBarButtonItem) {
        let cell = nameCell()
        name = cell.nameText.text
        performSegue(withIdentifier: "passData", sender: self)
    }
}

这是带有 TextField 的 Cell 类:

class NameCell: UITableViewCell {

    @IBOutlet weak var nameText: UITextField!

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

有什么想法吗?

【问题讨论】:

  • 你要做什么任务。解释一下。
  • 目前还不清楚你要完成什么。只有一行的表格视图没有意义。表格视图将显示什么?为什么这个类被命名为一次nameCell,然后是NameCell?请详细解释代码应该做什么。
  • 这只是一个示例项目。看起来我在写问题时犯了一个错误。当然名字是nameCell。在我的真实项目中,我有更多的行,其中包含 textFields 和其他内容。这就像添加一个新的联系人,我可以在其中输入一个名字。另一个带有 TableView 的 ViewController 获取数据并显示它。
  • @DilanAnuruddha
  • @vadian 信息够了吗?

标签: swift xcode uitableview textfield


【解决方案1】:

实际上这与我在您上一个问题中的建议相同。

在单元格中添加回调,并在调用文本字段委托方法时调用它。不要忘记在 Interface Builder 中连接文本字段委托。

请始终以大写字母开头的类和结构命名

class NameCell: UITableViewCell, UITextFieldDelegate {

    @IBOutlet weak var nameText: UITextField!

    var callback : ((UITableViewCell, String) -> Void)?

    func textFieldDidEndEditing(_ textField: UITextField) {
        callback?(self, nameText.text)
    }
}

如果您有不止一行,则需要声明一个数据源数组来维护文本字段的值。这是 4 行的示例

var values = ["", "", "", ""]

这些是数据源方法,在cellForRow回调更新模型

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return values.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "nameCell", for: indexPath) as! NameCell
    cell.nameText.text = values[indexPath.row]
    cell.callback = { [unowned self] cCell, cName in
        let currentIndexPath = tableView.indexPath(for: cCell)!
        self.values[currentIndexPath.row] = cName
    }
    return cell
}

好处是您独立于clickSave 方法中的单元格。从数据源数组中获取值。并且可以将名称作为sender 参数传递给prepare(for segue

@IBAction func clickSave(_ sender: UIBarButtonItem) {
    let name = values[0]
    performSegue(withIdentifier: "passData", sender: name)
}

【讨论】:

  • 感谢您快速详细的回答。它仍然无法正常工作。 Name 仍然是一个空字符串,因为 Values[0] 从不添加来自 textField 的文本。您的代码中也存在一些错误。不应该是 self.values[currentIndexPath.row] = cName 吗?
  • 您是否连接了文本字段委托?是否调用了textFieldDidEndEditing?你是对的,self. 不见了。如果您要创建表单,请考虑使用 Eureka 库。它非常强大。
  • 好的,我发现了我的问题。文本字段委托与 View Controller 连接,而不是与 Table View Cell 连接。谢谢瓦迪安
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-08-14
  • 1970-01-01
  • 2021-03-10
  • 1970-01-01
  • 1970-01-01
  • 2016-10-28
  • 1970-01-01
相关资源
最近更新 更多