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!
The cell probably doesn't exist - try NSLogging its value or making sure it's created properly.
Just have a try:
[tableView beginUpdates];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With