如何从 UItextfield 获取用户输入并将其添加到数组?

How to get user input from UItextfield and add it to an array?

我在 swift 工作。我有一个位于表格视图单元格中的文本字段。我试图将每个文本字段的文本存储在 tableview 中,以便当用户添加或删除一行,然后 tableview 重新加载数据时,文本字段会填充适当的数据。

我尝试添加一个 textfielddidendeditting 函数,但由于某种原因它没有被调用。

编辑:

这是我的代码:

表格视图控制器:

import UIKit

var rowCount = 0
var textArray = [String]()

class TableViewController: UITableViewController {


override func viewDidLoad() {
    super.viewDidLoad()
}


override func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

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

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

    return cell
}

@IBAction func addRow(_ sender: Any) {
    rowCount = rowCount + 1
    textArray.append("")
    tableView.reloadData()
}

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == .delete {
        rowCount = rowCount - 1
        textArray.remove(at: indexPath.row)
        tableView.deleteRows(at: [indexPath], with: .fade)

    } else if editingStyle == .insert {
    }
}


}

tableViewCell:

import UIKit

class TableViewCell: UITableViewCell, UITextFieldDelegate {


@IBOutlet weak var textField: UITextField!

override func awakeFromNib() {
    super.awakeFromNib()
    textField.delegate = self

}

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

    // Configure the view for the selected state
}

func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
    if let myIndexPath = tableView.indexPathForSelectedRow {
        let newText = textField.text
        textArray.insert(newText, at: myIndexPath)
    }

    return true
}

}

据我建议(没有给出任何代码洞察力),您可以执行以下操作:

  1. 在您的单元格中使用回调,每次文本字段结束编辑时都会调用该回调:

.

class MyTableViewCell: UITableViewCell {
       var textHasChanged: ((String) -> Void)?
       ...
    } 
    extension MyTableViewCell: UITextFieldDelegate {
    // this gets called every time the user ends editing the text field
    func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
            let newValue = textField.text //here you have your value

            // now save it to your data source
            self.textHasChanged(newValue)
        }
    }
  1. 在初始值设定项或 awakeFromNib() 函数中(取决于您的用法),将文本字段的 .delegate 属性 设置为 self

  2. 现在,要让每个单元格显示来自数据源的值并将更改的文本应用到您的 tableView 数据源,请将以下行添加到您的 UITableViewController:

.

class MyTableViewController: UITableViewController {
    var textArray: [String] = ["abc", "def"]
    ...
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "myCell", for: indexPath)

        cell.textField.text = textArray[indexPath.row]
        cell.textHasChanged = { (newValue) in
            self.textArray[indexPath.row] = newValue
        }
        return cell
}

如有其他问题,请发表评论