2015-04-17 45 views
0

我有一个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 
    } 
} 
+1

您是否尝试更改顺序?第一次删除并保存和'[tableView beginUpdates]' – thorb65

+0

只是尝试颠倒删除的东西的顺序。 'managedObjectContext'东西第一,然后'tableView'没有运气。我也尝试在方法的末尾放置'[tableView beginUpdates]',在方法末尾放置'[tableView endUpdates]'('if'语句之外)。我在'[tableView endUpdates]'处发生了同样的错误。 – Adrian

回答

1

想必您的tableView数据源方法使用self.locationCaregories。如果是这样,问题是你不会从该数组中删除相关的Category(尽管你从managedObjectContext中删除它,它仍然在数组中)。

现在,因为self.locationCategories是一个NSArray,它是不可变的,所以你不能只删除相关的项目。删除对象后,您可以立即重新执行提取操作,但这看起来效率不高。我建议你用self.locationCategories作为NSMutableArray。您将需要修改属性定义,并在viewWillAppear行,你先设置:在commitEditingStyle方法

NSArray *categories = [self.managedObjectContext executeFetchRequest:fetchRequest error:nil]; 
    self.locationCategories = [categories mutableCopy]; 

然后,您可以从阵列中删除相关的对象:

LocationCategory *categoryToDelete = [self.locationCategories objectAtIndex:indexPath.row]; 
    [self.locationCategories removeObjectAtIndex:indexPath.row]; 

正如thorb65所说的,在更新表之前,将代码移动到方法末尾 - 您应该获取正确的数据。

+0

谢谢@ thorb65和@pbasdf的回复。在你的答案之间,我得到了这个东西的工作。我认为'NSArray'是问题的一部分。这里提到的解决方案并没有让它自己工作,但是出于绝望,我从'viewWillAppear'中取出了一些代码,并且在从'managedObjectContext'中删除该项目后重新创建了'self.locationCategories'。谢谢你们对此的帮助。 – Adrian

+0

重新阅读和更新的代码描述为@pbasdf - 更清洁和更短。再次感谢你! – Adrian

相关问题