Assertion failure when updating tableView

ios, objective-c

Solution

The problem is in your `didSelectRowAtIndexPath` method. You have:

[tableView beginUpdates];

//lets delete the control cell, either the user tapped the same row twice or tapped another row
if(indexPathToDelete){
    [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPathToDelete]
                          withRowAnimation:UITableViewRowAnimationNone];
}
//lets add the new control cell in the right place
if(self.controlRowIndexPath){
    [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:self.controlRowIndexPath]
                          withRowAnimation:UITableViewRowAnimationNone];
}

//and we are done...
[tableView endUpdates];

Before you make any calls to tell the table to add or remove any rows, you must update your data source with by adding or removing data. The table will check how many sections and rows there are before and after your add/remove rows. The number of sections and rows after the change must properly reflect how much data you add/remove with how many rows you add/remove.

And of course you must implement the `numberOfRowsInSection` method.

Problem

Note: when I tap the row, then the app crashes. I'm trying to implement adding a new cell on a user's tap. I found that there was a similar example in WWDC 2011's table view demonstration. Here's my code from my table view. Here is the error: ``` 2013-03-19 20:04:28.672 Project[51229:c07] *** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-2380.17/UITableView.m:1070 ``` Here is my code from the table view. ``` @interface MyPFQueryTableViewController : PFQueryTableViewController <PFLogInViewControllerDelegate, PFSignUpViewControllerDelegate> @property (nonatomic, strong) NSIndexPath *controlRowIndexPath; @property (nonatomic, strong) NSIndexPath *tappedIndexPath; @implementation MyPFQueryTableViewController { ListItemObject *listDetail; } @synthesize controlRowIndexPath; @synthesize tappedIndexPath; -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) { PFObject *object = [self.objects objectAtIndex:indexPath.row]; [object deleteInBackgroundWithBlock:^(BOOL succeeded, NSError *error) { [self loadObjects]; }]; } } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object { static NSString *CellIdentifier = @"listCell"; PFTableViewCell *cell = (PFTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"listCell"]; if (cell == nil) { cell = [[PFTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } // Configure the cell cell.textLabel.text = [object objectForKey:self.textKey]; //cell.imageView.file = [object objectForKey:self.imageKey]; return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [super tableView:tableView didSelectRowAtIndexPath:indexPath]; //if user tapped the same row twice let's start getting rid of the control cell if([indexPath isEqual:self.tappedIndexPath]){ [tableView deselectRowAtIndexPath:indexPath animated:NO]; } //update the indexpath if needed... I explain this below indexPath = [self modelIndexPathforIndexPath:indexPath]; //pointer to delete the control cell NSIndexPath *indexPathToDelete = self.controlRowIndexPath; //if in fact I tapped the same row twice lets clear our tapping trackers if([indexPath isEqual:self.tappedIndexPath]){ self.tappedIndexPath = nil; self.controlRowIndexPath = nil; } //otherwise let's update them appropriately else{ self.tappedIndexPath = indexPath; //the row the user just tapped. //Now I set the location of where I need to add the dummy cell self.controlRowIndexPath = [NSIndexPath indexPathForRow:indexPath.row + 1 inSection:indexPath.section]; } //all logic is done, lets start updating the table [tableView beginUpdates]; //lets delete the control cell, either the user tapped the same row twice or tapped another row if(indexPathToDelete){ [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPathToDelete] withRowAnimation:UITableViewRowAnimationNone]; } //lets add the new control cell in the right place if(self.controlRowIndexPath){ [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:self.controlRowIndexPath] withRowAnimation:UITableViewRowAnimationNone]; } //and we are done... [tableView endUpdates]; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { if([indexPath isEqual:self.controlRowIndexPath]){ return 45; //height for control cell } return 70; //height for every other cell } - (NSIndexPath *)modelIndexPathforIndexPath:(NSIndexPath *)indexPath { int whereIsTheControlRow = self.controlRowIndexPath.row; if(self.controlRowIndexPath != nil && indexPath.row > whereIsTheControlRow) return [NSIndexPath indexPathForRow:indexPath.row - 1 inSection:0]; return indexPath; } @end ```

Original source