【问题标题】:How to get user input from UItextfield and add it to an array?如何从 UItextfield 获取用户输入并将其添加到数组中?
【发布时间】:2019-01-07 21:07:37
【问题描述】:

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

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

编辑:

这是我的代码:

tableViewController:

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
}

}

【问题讨论】:

  • "我尝试添加一个 textfielddidendeditting 函数,但由于某种原因它没有被调用" 但是除非您向我们展示您的代码,否则我们无法告诉您原因是什么。
  • 另外,这是一个非常常见的需求(将表格视图单元格中的文本字段中的文本传递到表格视图的模型数据中)。问之前有没有试过搜索?

标签: ios swift xcode textfield


【解决方案1】:

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

  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
}

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

【讨论】:

  • 嘿木里。感谢您的建议。我的第一个错误是我忘记将 .delegate 设置为 self。现在我已经修复了这个简单的错误,我想将新的文本字段文本设置为数组中的特定索引,该索引与文本字段所在的行相同。这里的问题是我不知道如何获取 indexPath在 tableViewCell 内时当前行的 .row。因此,当我定义 myIndexPath 时出现错误。有什么建议吗?
  • 查看更新的答案。您犯的错误是尝试从您的单元格访问 tableView 及其数据源。您必须像我一样使用另一个委托或回调。将步骤 1 中的回调添加到 UITableViewCellFeel,调用 textFieldShouldEdit 中的函数并将步骤 3 应用于 TableViewController。欢迎提出更多问题。
  • @Ilya 你的问题是什么状态?你解决了吗?您需要进一步的帮助吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-05-18
  • 2017-12-25
  • 1970-01-01
  • 2020-07-09
  • 2011-11-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多