Swift: retrieving text from a UITextField in a custom UITableViewCell and putting it in an array

匿名 (未验证) 提交于 2019-12-03 02:12:02

问题:

I'm making a very simple app where the user enters the number of people in the first Screen.

In the second screen it generates a number of UITableViewCell based on the number the user entered in the first screen. The UITableViewCell have a UITextField in them and I'm trying to store the data entered in those fields in an array once the user clicks to go to the third screen.

How can I do that?
Thanks in advance!

Edit: I'm using the storyboard.

Here is what the code that calls for the custom UITableViewCell looks like for my UIViewController:

func tableView(tableView:UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{      var cell: EditingCell = tableView.dequeueReusableCellWithIdentifier("Cell") as EditingCell      if indexPath.row % 2 == 0{         cell.backgroundColor = UIColor.purpleColor()     } else {         cell.backgroundColor = UIColor.orangeColor()     }      let person = arrayOfPeople[indexPath.row]      cell.setCell(person.name)       return cell }

Here is what the code for the UITableViewCell looks like:

class EditingCell: UITableViewCell{      @IBOutlet weak var nameInput: 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     }       func setCell(name:String){         self.nameInput.placeholder = name     } }

回答1:

There is a problem with your approach if the number of rows in your table exceeds the number that can fit on screen. In that case, the cells that scroll off-screen will be re-used, and the contents of the nameInput textField will be lost. If you can be sure that this will never happen, use the following code (in the method that handles button taps) to compose your array:

        var arrayOfNames : [String] = [String]()         for var i = 0; i<self.arrayOfPeople.count; i++ {             let indexPath = NSIndexPath(forRow:i, inSection:0)             let cell : EditingCell? = self.tableView.cellForRowAtIndexPath(indexPath) as EditingCell?             if let item = cell?.nameInput.text {                 arrayOfNames.append(item)             }         }         println("\(arrayOfNames)")

Alternatively....

However, if it is possible that cells will scroll off-screen, I suggest a different solution. Set the delegate for the nameInput text fields, and then use the delegate methods to grab the names as they are entered.

First, add variables to your view controller, to hold the array and the row number of the text field currently being edited.

    var arrayOfNames : [String] = [String]()     var rowBeingEdited : Int? = nil

Then, in your cellForRowAtIndexPath method, add:

    cell.nameInput.text = "" // just in case cells are re-used, this clears the old value     cell.nameInput.tag = indexPath.row     cell.nameInput.delegate = self

Then add two new functions, to catch when the text fields begin/end editing:

func textFieldDidEndEditing(textField: UITextField) {     let row = textField.tag     if row >= arrayOfNames.count {         for var addRow = arrayOfNames.count; addRow <= row; addRow++ {             arrayOfNames.append("") // this adds blank rows in case the user skips rows
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!