我有一个UITableView
显示中的实体的属性,这些实体由NSFetchRequest
创建。我的导师告诉我为我的目的,我不需要fetchedResultsController
--so 我不需要将NSFetchedResultsControllerDelegate
方法添加到我的UITableViewController
类。他的比喻是将保时捷引擎装入大众甲壳虫。从managedObjectContext和tableView中删除NSManagedObject
我的tableView有2个部分(0和1)。一切工作正常,直到我试图从第1部分删除对象(第0部分是静态的)。
有了这个commitEditingStyle代码时,应用程序崩溃,他的错误:
*** Assertion failure in -[UITableView _endCellAnimationsWithContext:],
/SourceCache/UIKit_Sim/UIKit-3347.40/UITableView.m:1623
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete && indexPath.section == 1) {
// Delete cell
[tableView beginUpdates];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];
// Delete LocationCategory from managedObjectContext
LocationCategory *categoryToDelete = [self.locationCategories objectAtIndex:indexPath.row];
[self.managedObjectContext deleteObject:categoryToDelete];
[self.managedObjectContext save:nil];
} else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
当我重新启动应用程序时,locationCategory
(我NSManagedObject
)是managedObjectContext
不再。我知道我在更新tableView
时遇到问题,但我不知道如何解决此问题。我挖了一遍,尝试了一堆潜在的解决方案,但没有任何办法可以解决崩溃问题。
下面是我的阵列创建:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.title = @"Select a Category";
// Core Data
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
self.managedObjectContext = [appDelegate managedObjectContext];
// Fetch LocationCategories & dump them in an array
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:[NSEntityDescription entityForName:@"LocationCategory" inManagedObjectContext:self.managedObjectContext]];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"categoryName" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:&sortDescriptor count:1];
[fetchRequest setSortDescriptors:sortDescriptors];
NSArray *categories = [self.managedObjectContext executeFetchRequest:fetchRequest error:nil];
self.locationCategories = categories;
[self.tableView reloadData];
}
更新:
这里看了一下类似的代码,在结束的时候,它的工作:
// This was previously an NSArray. It needs to be an NSMutableArray
@property (nonatomic, strong) NSMutableArray *locationCategories;
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete && indexPath.section == 1) {
// Delete LocationCategory from managedObjectContext
LocationCategory *categoryToDelete = [self.locationCategories objectAtIndex:indexPath.row];
[self.managedObjectContext deleteObject:categoryToDelete];
[self.locationCategories removeObjectAtIndex:indexPath.row];
[self.managedObjectContext save:nil];
// Delete cell
[tableView beginUpdates];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];
} else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
您是否尝试更改顺序?第一次删除并保存和'[tableView beginUpdates]' – thorb65
只是尝试颠倒删除的东西的顺序。 'managedObjectContext'东西第一,然后'tableView'没有运气。我也尝试在方法的末尾放置'[tableView beginUpdates]',在方法末尾放置'[tableView endUpdates]'('if'语句之外)。我在'[tableView endUpdates]'处发生了同样的错误。 – Adrian