title: Incorrect indexPath against UITableView after calling reloadData from textFieldDidEndEditing
body: <p>So I'm trying to emulate the way in which the standard Apple code works on the contacts app when managing telephone numbers.  Specifically here, I'm working on deleting a row from a tableview if its empty and the user navigates to any other row</p>
  
  <p>My problem is that as the tableview reload causes the UITextField to resign its responder, I need to set the responder again for the text field navigated to by the user</p>
  
  <p>I have the UITextField delegate and am handling the usual <code>textFieldShouldBeginEditing , textFieldDidBeginEditing , textFieldShouldEndEditing , textFieldDidEndEditing</code></p>
  
  <p>In order to handle the functionality, my code is within <code>textFieldDidEndEditing</code>, whereby I remove the data from the tableview array, and as the tableview has 2 sections, I am calling: </p>
  
  <pre><code>[MyTableView reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationNone];
  </code></pre>
  
  <p>During <code>textFieldDidBeginEditing</code> I save the indexPath of the textField being edited using:</p>
  
  <pre><code>EditableCustomCell *textFieldCell = (EditableCustomCell *)[[textField superview] superview];
  NSIndexPath *indexPath = [MyTableView indexPathForCell:(EditableCustomCell *)textFieldCell];
  responderIndexPath = [NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section];
  </code></pre>
  
  <p>I then use the following code to set the correct rows textField to be the first responder:</p>
  
  <pre><code>EditableCustomCell *customCell = (EditableCustomCell *)[MyTableView cellForRowAtIndexPath:responderIndexPath];
  [customCell.editableTextField becomeFirstResponder];
  </code></pre>
  
  <p>Everything seems fine until near the end of the processing, when all of a sudden <code>textFieldDidBeginEditing</code> starts to return section 0 row 0 for the indexPath (even though when examining the tag value or text contained return the correct values for the textfield)</p>
  
  <p>Below is a log from the start of the process explained above:</p>
  
  <pre><code>- textFieldDidEndEditing started  &lt;-- start of delete processing
  - textFieldDidEndEditing - tableviewData - replacing object at index 1
      CustomTableViewController.deleteRow - delete called for indexPath section 1 ... row 1
  - reloading MyTableView
  - CustomTableViewController-cellForRowAtIndexPath started
      CustomTableViewController-cellForRowAtIndexPath section=1 row=0
      CustomTableViewController-cellForRowAtIndexPath ending
      CustomTableViewController-cellForRowAtIndexPath section=1 row=1
      CustomTableViewController-cellForRowAtIndexPath ending
      CustomTableViewController-cellForRowAtIndexPath section=1 row=2
      CustomTableViewController-cellForRowAtIndexPath ending
  - textFieldShouldBeginEditing started
      indexPath for textFieldShouldBeginEditing is : section 1 row 1
  - textFieldShouldBeginEditing ending
  - textFieldDidBeginEditing started
      indexPath for textFieldDidBeginEditing is : section 1 row 1 text 3 tag 1
  - textFieldDidBeginEditing ending
  - textFieldDidEndEditing ending  &lt;-- end of delete processing
  - textFieldDidBeginEditing started
  - textFieldDidBeginEditing ... setting responderIndexPath section 0 row 0
      indexPath for textFieldDidBeginEditing is : section 0 row 0 text 123 tag 0
  - textFieldDidBeginEditing ending
  </code></pre>
  
  <p>As can be seen from the last part of the log, after <code>textFieldDidEndEditing</code> completes, <code>textFieldDidBeginEditing</code> is called but returns section 0 and row 0 (the rows stay displayed and visible throughout)</p>
  
  <p>I can neither understand why this is called, or why it is not returning the correct indexPath.  As can be seen, the text is returned for the value (in this case the entered value of 123) and I have verified this with other data and other rows (for both text and tag for the textfield)</p>
  
  <p>Perhaps my setting of <code>becomeFirstReponsder</code> within <code>textFieldDidEndEditing</code> is incorrect, although if that's true, I'm at a loss as to where to process this</p>
  
  <p>Hoping someone out there with a better understanding of this can help me out, as you can tell, I've been through this for several hours without any resolution</p>
  
  <p>Thanks
  Izzy</p>
  
  <p><strong>EDIT 1:</strong> <em>In the code, all that is called is <code>becomeFirstReponder</code> before <code>textFieldDidEndEditing</code> completes.  When you look at the log after <code>cellForRowAtIndexPath</code> has completed, it enters and exists the textfield TWICE, once for the row that was below the row just removed, and then again when it returns 0 for the section/row of the indexPath ?  I am failing to understand what sequence of events is causing this post table reload firing of methods</em></p>
  
  <p><strong>EDIT 2:</strong> <em>Is it just me, or does it seem really strange that there is NO <code>textfieldSHOULDbeginEditing</code> prior to the <code>textFieldDidBeginEditing</code> at the end of the log ?  Is it that I am screwing with the internal process by reloading the table during <code>textFieldDidEndEditing</code> ?  Is there a better place to do this (ie. remove a row and reload the tableview to show the UI update) ?</em></p>