Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Attempt to create two animations for cell" Crash on ios

Here is my first post on stackoverflow. Any of your help will be greatly appreciated!

I am totally new to ios development. As a practice, I am trying to develop a simple app that keeps track of diffrent items in my lab. Each item has a to-one relationship to its location and each location has a to-many relationship to different items.

I have a UITableViewController(LocationListTableViewController) that uses NSFectchedResultController as its data source for the tableview. The tableview controller is also conformed to NSFetchedResultController Delegate to keep all the NSManagedObject updated.

Once a location on the LocationListTableViewController is tapped, a DetailedTableViewController will be pushed and shows the items at that location. This DetailedTableViewController has an NSUndoManager instance variable to support canceling of changes. I use a NSMutableArray as the data source of the DetailedTableViewController:

NSSet *items = self.location.items;
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"itemName" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
NSArray *array = [[items allObjects]sortedArrayUsingDescriptors:sortDescriptors];
// itemList is the data source for the table view
self.itemList = [array mutableCopy];

When the "Edit" button is tapped, Users can delete items and those item's location is set to nil. The deleted Items are transferred to a "changedItem" NSMutableArray so that the change can be canceled:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{

    if (!changedItems)
       changedItems = [[NSMutableArray alloc] init];

    // Set the company attribute of selected company to nil
    Item *editingItem = [itemList objectAtIndex:indexPath.row];
    editingItem.company = nil;

    // Add to changedItem array to support canceling
    [changedItems addObject:editingItem];
    // Remove from the data source array
    [itemList removeObject:editingItem];

    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}

I put a Cancel button as the left bar button item, and when it's tapped:

 [undoManager endUndoGrouping];
 NSManagedObjectContext *moc = self.location.managedObjectContext;
 [moc rollback];

 // Add item from changedItem array back to itemList array (data source array)
 if ([changedItems count] > 0) {
      [itemList addObjectsFromArray:changedItems];
      [itemList sortUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
  }

Here comes the problem. When I delete more than one item and try to cancel these change, the app does not crash but I get the error message:

*** Assertion failure in -[_UITableViewUpdateSupport _setupAnimationsForNewlyInsertedCells], /SourceCache/UIKit_Sim/UIKit-1448.89/UITableViewSupport.m:599

Serious application error.  An exception was caught from the delegate of NSFetchedResultsController during a call to -controllerDidChangeContent:.  Attempt to create two animations for cell with userInfo (null)

When only deleting one item, the app seems fine.

This is very anoying. Is there anybody could help me with this?

Many thanks!

like image 636
sean_xia Avatar asked Apr 20 '11 03:04

sean_xia


2 Answers

The cell probably doesn't exist - try NSLogging its value or making sure it's created properly.

like image 194
Koh Jing Yu Avatar answered Oct 21 '22 03:10

Koh Jing Yu


Just have a try:

[tableView beginUpdates];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];
like image 2
simalone Avatar answered Oct 21 '22 03:10

simalone