Question

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

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

I have the UITextField delegate and am handling the usual textFieldShouldBeginEditing , textFieldDidBeginEditing , textFieldShouldEndEditing , textFieldDidEndEditing

In order to handle the functionality, my code is within textFieldDidEndEditing, whereby I remove the data from the tableview array, and as the tableview has 2 sections, I am calling:

[MyTableView reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationNone];

During textFieldDidBeginEditing I save the indexPath of the textField being edited using:

EditableCustomCell *textFieldCell = (EditableCustomCell *)[[textField superview] superview];
NSIndexPath *indexPath = [MyTableView indexPathForCell:(EditableCustomCell *)textFieldCell];
responderIndexPath = [NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section];

I then use the following code to set the correct rows textField to be the first responder:

EditableCustomCell *customCell = (EditableCustomCell *)[MyTableView cellForRowAtIndexPath:responderIndexPath];
[customCell.editableTextField becomeFirstResponder];

Everything seems fine until near the end of the processing, when all of a sudden textFieldDidBeginEditing 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)

Below is a log from the start of the process explained above:

- textFieldDidEndEditing started  <-- 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  <-- 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

As can be seen from the last part of the log, after textFieldDidEndEditing completes, textFieldDidBeginEditing is called but returns section 0 and row 0 (the rows stay displayed and visible throughout)

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)

Perhaps my setting of becomeFirstReponsder within textFieldDidEndEditing is incorrect, although if that's true, I'm at a loss as to where to process this

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

Thanks Izzy

EDIT 1: In the code, all that is called is becomeFirstReponder before textFieldDidEndEditing completes. When you look at the log after cellForRowAtIndexPath 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

EDIT 2: Is it just me, or does it seem really strange that there is NO textfieldSHOULDbeginEditing prior to the textFieldDidBeginEditing at the end of the log ? Is it that I am screwing with the internal process by reloading the table during textFieldDidEndEditing ? Is there a better place to do this (ie. remove a row and reload the tableview to show the UI update) ?

Was it helpful?

Solution

OK, to answer my own question ...

After alot of debugging, it seems that ...

I'm inteferring with the standard UI workflow by reloading the tableview during textfieldDidEndEditing

Calling reloaddata destroys the tableview cells (and all objects contained within the cells ie. UITextFields, UILabels, etc) and recreates them, causing

By moving this call out to a standalone button, it functioned as expected without the indexPath returning 0 (I still dont fully understand the call stack when it gets to this point, but hey)

Now my BIG problem is deciding when to programatically call the reload, as I want it driven off the event of leaving the text field ... I feel another question coming on :\

Hope my ramblings help someone out in the future that tries to do a similar thing ...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top