Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 707
Skoota Avatar asked Dec 17 '12 01:12

Skoota


3 Answers

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.

like image 96
matt Avatar answered Oct 17 '22 17:10

matt


i am also faced the same problem. while keyboard is present you need to call resignFirstResponder first then call UITableview reload method after

(void)textFieldDidEndEditing:(UITextField *)textField

delegate method executed or inside:

(void)textFieldDidEndEditing:(UITextField *)textField{
    [tableview reloadData];
}
like image 1
Ashokkumar.S S Avatar answered Oct 17 '22 15:10

Ashokkumar.S S


My crash was from an editable UIWebView inside of a UICollectionViewCell. The following fixed it:

[self.view endEditing:YES]

like image 1
kgaidis Avatar answered Oct 17 '22 16:10

kgaidis