“尝试为单元格创建两个动画”在ios上崩溃

时间:2011-04-20 03:32:21

标签: iphone objective-c core-data uitableview

这是我关于stackoverflow的第一篇文章。非常感谢您的任何帮助!

我对ios开发完全陌生。作为一种做法,我正在尝试开发一个简单的应用程序,在我的实验室中跟踪不同的项目。每个项目与其位置具有一对一的关系,并且每个位置与不同的项目具有多对多的关系。

我有UITableViewController(LocationListTableViewController)使用NSFectchedResultController作为tableview的数据源。 tableview控制器也符合NSFetchedResultController委托,以保持所有NSManagedObject更新。

点击LocationListTableViewController上的某个位置后,系统会推送DetailedTableViewController并显示该位置的项目。此DetailedTableViewController具有NSUndoManager实例变量,以支持取消更改。我使用NSMutableArray作为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];

点击“编辑”按钮后,用户可以删除项目,并将这些项目的位置设置为零。已删除的项目将转移到“changedItem”NSMutableArray,以便可以取消更改:

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

我将一个取消按钮作为左侧栏按钮项目,并点击它时:

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

这就是问题所在。当我删除多个项目并尝试取消这些更改时,应用程序不会崩溃但我收到错误消息:

*** 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)

当只删除一个项目时,该应用似乎没问题。

这非常令人讨厌。有没有人可以帮我这个?

非常感谢!

2 个答案:

答案 0 :(得分:3)

单元格可能不存在 - 尝试NSLogging其值或确保正确创建它。

答案 1 :(得分:1)

试一试:

[tableView beginUpdates];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];
相关问题