问题描述
我正在快速工作.我有一个位于 tableview 单元格中的文本字段.我试图将每个文本字段的文本存储在 tableview 中,以便在用户添加或删除一行时它们,然后 tableview 重新加载数据,使文本字段保持填充适当的数据.
I am working in swift. I have a textfield that is in a tableview cell. I am trying to store the text of each text field in the tableview so they when the user adds or deletes a row, and then the tableview reloads data, that the text fields stay filled in with the appropriate data.
我尝试添加一个 textfielddidenditting 函数,但由于某种原因它没有被调用.
I tried adding a textfielddidendeditting function but for some reason it is not being called.
这是我的代码:
tableViewController:
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:
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
}
}
推荐答案
据我所知(没有给出任何代码见解),您可以执行以下操作:
As far as I could suggest (without any code insight given) you could do the following:
- 在您的单元格中使用回调,每次文本字段结束编辑时都会调用该回调:
.
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)
}
}
在初始化程序或
awakeFromNib()
函数(取决于您的使用情况)中,将文本字段的.delegate
属性设置为self
In a initializer or in
awakeFromNib()
function (depends on your usage), set the.delegate
property of the textfield toself
现在,要让每个单元格显示来自数据源的值并将更改后的文本应用到您的 tableView 数据源,请将以下几行添加到您的 UITableViewController:
Now, to have each cell display the value from the datasource and to apply the changed text to your tableView datasource, add the following lines to your 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
}
如果您还有其他问题,请发表评论
Just comment if you have further questions
这篇关于如何从 UItextfield 获取用户输入并将其添加到数组中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!