Attempting to delete a line containing the first responder who refused to resign

I have an iOS 6 project that implements a UITableView . Each cell in the table view contains a UITextField that allows the user to enter information. If a user deletes a text field or deletes all data from the field (ie [textfield length] == 0 ) when they click on another cell (text field), it removes the previous cell (text field) from the table view, because it empty - this avoids the empty cells accumulated in the form of a table.

All this is done using the -textFieldEditingDidEnd: method, which fires the UIControlEventEditingDidEnd event for text fields:

 - (void)textFieldEditingDidEnd:(UITextField *)textField { NSIndexPath *indexPath = // Index path of the row in the table view if ([textField.text length] == 0) { // Delete the cell from the table view [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; } } 

However, when the code starts, the application crashes with the following message on the console:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Attempt to delete row containing first responder that refused to resign'

I have never seen this post before, and there seems to be no special links to it when searching the Internet. I would appreciate any suggestions for fixing this issue.

+4
source share
7 answers

I have never seen this message before, but my immediate impulse, if I saw it, would be: try delayed performance. Even something as simple as this might be an interesting experiment:

 dispatch_async(dispatch_get_main_queue(), ^{ [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; }); 

My thought here is that do not try to delete the line while the text field is still reporting (i.e. while textFieldEditingDidEnd is still working); let it give runloop a chance to complete its cycle.

+11
source

I also ran into the same problem. while the keyboard is present, you need to call resignFirstResponder first and then call UITableview reload the method after

 (void)textFieldDidEndEditing:(UITextField *)textField 

the delegation method is executed either internally:

 (void)textFieldDidEndEditing:(UITextField *)textField{ [tableview reloadData]; } 
+1
source

My failure came from an editable UIWebView inside a UICollectionViewCell . After that fixed:

[self.view endEditing:YES]

+1
source

I encountered the same NSInternalInconsistencyException exception. My solution was as follows:

 textView.selectedTextRange = nil; // clear the selected text [textView resignFirstResponder]; 
-1
source

Get this error when deleting a UITableViewCell containing a UITextView from keyboard UIKeyboardWillHideNotification .

Fixed a move logic in UIKeyboardDidHideNotification

-1
source

I ran into this problem when my textFieldShouldEndEditing method returned NO in some situations

-1
source

I also experienced the same problem. Despite the fact that after calling "resignFirstResponer" and "endEditing", it fails.

When I tried using the @matt approach, it works fine. Thanks matte.

It seems that since we add “deleteRowsAtIndexPaths:” to the GCD queue, the queue will make sure that the previous task was completed in the queue, and then only run the next task in main_queue. Therefore, there is no need to add a delay.

-1
source

All Articles