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

I am making a very simple application in which the user enters the number of people on the first screen.

On the second screen, a UITableViewCell number is generated based on the number entered by the user on the first screen. UITableViewCell have a UITextField in them, and I'm trying to save the data entered into these fields in an array as soon as the user clicks on the third screen.

How can i do this?
Thanks in advance!

Edit: I am using a storyboard.

Here is what the code that calls the custom UITableViewCell 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's what the code for 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 } } 
+8
ios uitextfield uitableview
source share
1 answer

There is a problem with your approach if the number of rows in the table exceeds the number that can fit on the screen. In this case, cells that scroll outside the screen will be reused, and the contents of the nameInput text box will be lost. If you can be sure that this will never happen, use the following code (in a method that processes short buttons) 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)") 

As an alternative....

However, if it is possible that the cells will scroll behind the scenes, I propose another solution. Set a delegate for the nameInput text fields, and then use the delegate methods to grab the names as you type them.

First, add variables to the view controller to hold the array and line number of the text field 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 text fields start / end:

 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 } } arrayOfNames[row] = textField.text rowBeingEdited = nil } func textFieldDidBeginEditing(textField: UITextField) { rowBeingEdited = textField.tag } 

When the user clicks the button, they can still edit one of the names. To satisfy this, add the following to a method that processes button taps:

  if let row = rowBeingEdited { let indexPath = NSIndexPath(forRow:row, inSection:0) let cell : EditingTableViewCell? = self.tableView.cellForRowAtIndexPath(indexPath) as EditingTableViewCell? cell?.nameTextField.resignFirstResponder() } 

This forces textField to complete editing and, therefore, invokes the didEndEditing method, thereby saving the text in an array.

+16
source share

All Articles