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

发布于 2024-11-02 16:42:44 字数 2773 浏览 1 评论 0原文

这是我在 stackoverflow 上的第一篇文章。您的任何帮助将不胜感激!

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

我有一个 UITableViewController(LocationListTableViewController) ,它使用 NSFectchedResultController 作为表视图的数据源。 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];

当点击“编辑”按钮时,用户可以删除项目,并且这些项目的位置设置为 nil。删除的项目被转移到“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)

仅删除一项时,应用程序似乎正常。

这很烦人。有人可以帮我解决这个问题吗?

非常感谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

天冷不及心凉 2024-11-09 16:42:44

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

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

歌入人心 2024-11-09 16:42:44

尝试一下:

[tableView beginUpdates];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];

Just have a try:

[tableView beginUpdates];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文