IPhone application crash while deleting UITableView row

I have a UITableView that uses a singleton object as a data source.

I applied the following methods to allow the user to delete rows in a UITableView. But when I click the "Delete" button, the application crashes with an exception

Methods:

-(void)viewDidLoad { some initialization code----- UIBarButtonItem *editButton = [[UIBarButtonItem alloc] initWithTitle:@"Delete" style:UIBarButtonItemStyleBordered target:self action:@selector(toggleEdit:)]; self.navigationItem.rightBarButtonItem = editButton; [editButton release]; } -(IBAction)toggleEdit:(id)sender { [self.myOrderTable setEditing:!self.myOrderTable.editing animated:YES]; if(self.myOrderTable.editing) [self.navigationItem.rightBarButtonItem setTitle:@"Done"]; else { [self.navigationItem.rightBarButtonItem setTitle:@"Delete"]; } } -(void)tableView:(UITableView*)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath*)indexPath { NSUInteger row = [indexPath row]; [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; } 

However, I get the following exception when I try to click the delete button:

Validation error in - [UITableView _endCellAnimationsWithContext:], / SourceCache / UIKit_Sim / UIKit-1448.89 / UITableView.m: 995

The application terminated due to the unannounced exception "NSInternalInconsistencyException", reason: "Invalid update: invalid number of lines in section 1. The number of lines contained in an existing section after updating (1) must be equal to the number of lines contained in this section before updating (1 ), plus or minus the number of rows inserted or deleted from this section (inserted 0, 1 deleted).

 *** Call stack at first throw: ( 0 CoreFoundation 0x010305a9 __exceptionPreprocess + 185 1 libobjc.A.dylib 0x01184313 objc_exception_throw + 44 2 CoreFoundation 0x00fe8ef8 +[NSException raise:format:arguments:] + 136 3 Foundation 0x001153bb - [NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 116 4 UIKit 0x00398e8b -[UITableView(_UITableViewPrivate) _endCellAnimationsWithContext:] + 8420 5 UIKit 0x00387cf8 - [UITableView deleteRowsAtIndexPaths:withRowAnimation:] + 56 6 Restaurant 0x000117f9 - [MyOrderViewController tableView:commitEditingStyle:forRowAtIndexPath:] + 114 7 UIKit 0x00385037 -[UITableView(UITableViewInternal) animateDeletionOfRowWithCell:] + 101 8 UIKit 0x0031a4fd -[UIApplication sendAction:to:from:forEvent:] + 119 9 UIKit 0x003aa799 -[UIControl sendAction:to:forEvent:] + 67 10 UIKit 0x003acc2b -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 527 11 UIKit 0x003ab7d8 -[UIControl touchesEnded:withEvent:] + 458 ............. .......... ............. ) terminate called after throwing an instance of 'NSException' 

Where can I solve this problem?

+2
source share
3 answers

"The number of lines contained in an existing section after the update (1) must be equal to the number of lines contained in this section before the update (1), plus or minus the number of inserted rows or removed from this section (inserted 0, 1 deleted). ''

The error clearly says what is wrong. I would consider your numberOfRowsInSection method. You need to consider the row to be deleted. You cannot always return the same number of rows. If you delete one row from the table, your OffRowsInSection number should return the old return value minus 1.

For instance:

Let's say I use an array of strings to populate a tableview;

In my method numberOfRowsInSection will be used

 return [array count]; 

The cellForRowAtIndexPath method will use

 cell.textLabel.text = (NSString*)[array objectAtIndex:indexPath.row]; 

When I delete a row (e.g. row number 3) from tableView, I also delete the object from the array:

 [array removeObjectAtIndex:3]; 

This way, when numberOfRowsInSection is called again, the array is less than one object, and numberOfRowsInSection returns one number less than before, which takes into account the deleted row.

+4
source

Did you read the error message? It says:

reason: 'Invalid update: invalid row count in section 1.

So ... the number of rows in your TableView is now incorrect. Why?

The number of lines contained in an existing section after updating (1) must be equal to the number of lines contained in this section before updating (1) plus or minus the number of lines inserted or deleted from this section (Insert 0, 1 is deleted).

When deleting a row from a View table, you must also remove the corresponding object from the array of objects that appear in the View table. You didn’t do it. Thus, the UITableView throws an exception.

+2
source

Are you deleting an element from your data source in commitEditingStyle :? If there are 2 elements in the table data source and you delete the row in commitEditingStyle: you must also remove the element from your data source so that now there is 1 element in the tableview and the data source.

+1
source

Source: https://habr.com/ru/post/1213472/


All Articles