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

僤鯓⒐⒋嵵緔 提交于 2021-01-29 15:51:45

问题


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.

I tried adding a textfielddidendeditting function but for some reason it is not being called.

EDIT:

Here is my code:

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
}

}

回答1:


As far as I could suggest (without any code insight given) you could do the following:

  1. Use a callback in your cell, which gets called every time the textfield ends editing:

.

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. In a initializer or in awakeFromNib() function (depends on your usage), set the .delegate property of the textfield to self

  2. 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



来源:https://stackoverflow.com/questions/54081929/how-to-get-user-input-from-uitextfield-and-add-it-to-an-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!