Attempt to delete row containing first responder that refused to resign

丶灬走出姿态 提交于 2019-12-01 03:43:56

问题


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.


回答1:


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.




回答2:


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];
}



回答3:


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

[self.view endEditing:YES]




回答4:


I came across the same NSInternalInconsistencyException exception. My solution was the following:

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



回答5:


Got this error when removing a UITableViewCell that contained a UITextView from a keyboard hide UIKeyboardWillHideNotification notification.

Fix was to move the logic into UIKeyboardDidHideNotification




回答6:


I came across this issue when my method textFieldShouldEndEditing was returning NO in some situations




回答7:


I also experienced the the same problem. Even though after calling "resignFirstResponer" and "endEditing" , It is found to be crashing.

When I tried with @matt's approach , it is working fine. Thanks matt.

It seems like since we are adding the "deleteRowsAtIndexPaths:" into the GCD queue , the queue will make sure that the previous task is completed in the queue and then only start the next task in the main_queue. Hence no need to add any delay.



来源:https://stackoverflow.com/questions/13907335/attempt-to-delete-row-containing-first-responder-that-refused-to-resign

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!