我有一個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