iosuitableviewuitextfieldfirst-responder

Attempt to delete row containing first responder that refused to resign


I have an iOS 6-based project that implements a UITableView. Each cell in the table view contains a UITextField allowing the user to enter information. If the user clears a text field, or deletes all input from the field, (i.e. [textfield length] == 0) when they tap onto another cell (text field) it deletes the previous cell (text field) from the table view, as it's empty - this avoids empty cells accumulating in the table view.

This is all done using a method called -textFieldEditingDidEnd: which fires on the UIControlEventEditingDidEnd event for the 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 is fired the app 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 message before, and there do not seem to be particularly many references to it when searching the web. I would appreciate any suggestions on how to fix this issue.


Solution

  • I've never seen that message before, but my immediate impulse if I were to see 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, let's not try to delete the row while the text field is still reporting in (i.e. while textFieldEditingDidEnd is still running); let's give the runloop a chance to finish its cycle.