Saving textfield values in array in the same order as TableView Cell even after some cells are deleted
<p>I have a TableView that allows user to add cells for text entry.</p>

<p>I implemented textfield delegate method that watches the change in the cell's textfield and store the text value in array.</p>

<p>I currently assign <code>indexPath.row</code> of cellForRowAtIndexPath method as a tag value of cell's textfields. And I use that tag as the index of array for updating values. </p>

<p>But this approach causes issue once I delete some cells and add new cells to store new value. Values get stored in random index of array.</p>

<p>How do I save values in array in the same index order as the table cell even after some cells are deleted?</p>

<pre><code>var stepCount = 1
var stepOrder = ["1"]
var steps = [""]

@IBAction func stepTextFieldDidChange(sender: UITextField) {
    steps[stepTextField.tag] = sender.text!
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -&gt; UITableViewCell {

    var cell:UITableViewCell?

    if tableView == self.stepTableView {
        let aCell = tableView.dequeueReusableCellWithIdentifier("StepCell", forIndexPath: indexPath) as! StepCell
        stepTextField = aCell.getTextField()
        stepTextField.tag = indexPath.row
        aCell.stepTextField.delegate = self
        aCell.configureStepCell(stepOrder[indexPath.row], step: steps[indexPath.row])
        cell = aCell
    }

    return cell
}

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    if tableView == stepTableView {

        if editingStyle == .Delete {
            // Delete the row from the data source
            if stepOrder.count &gt; 1 {
                steps.removeAtIndex(indexPath.row)
                stepTableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
                stepTableView.reloadData()
            }   
        } else if editingStyle == .Insert {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
        }
    }
}

@IBAction func addStep(sender: AnyObject) {
    stepCount++
    stepOrder.append("\(stepCount)")
    steps.append("")
    stepTextField.becomeFirstResponder()
    stepTableView.reloadData()
}
</code></pre>

<p><a href="https://i.stack.imgur.com/Caujy.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Caujy.png" alt="enter image description here"></a></p>