2015-04-17 47 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

相關問題