2012-03-06 28 views
4

我正在使用NSFetchedResultsController。以前我有一個類似的問題,當數據庫沒有用於tableview的條目但創建了一個時,我發現必須至少有一個部分,所以我解決了這個問題。但現在它崩潰了,例如我有兩個部分,每個部分都有一行,我刪除了一行,所以部分應該消失 - >崩潰。它表示更新前的部分數量(2)不等於刪除的數量(0)。使用NSFetchedResultsController刪除節中的最後一行 - >崩潰,使用NSFetchedResultsController

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    // Return 1 if the fetchedResultsController section count is zero 
    return [[fetchedResultsController sections] count] ? : 1; 
} 

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { 
    // check if we really have any sections in the managed object: 
    if (!fetchedResultsController.sections.count) return @"Persoonlijk"; 

    id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController sections] objectAtIndex:section]; 
    return [sectionInfo name]; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    // check if we really have any sections in the managed object: 
    if (!fetchedResultsController.sections.count) return 0; 

    id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController sections] objectAtIndex:section]; 
    return [sectionInfo numberOfObjects]; 
} 

更新 方法,其中行被刪除:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     // Delete schedule 
     NSManagedObjectContext *context = [fetchedResultsController managedObjectContext]; 
     [context deleteObject:[fetchedResultsController objectAtIndexPath:indexPath]]; 

     // Save the context. 
     NSError *error = nil; 
     if (![context save:&error]) { 
      NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
      exit(-1); 
     } 
    } 
} 
+0

這是怎麼回事:return [[fetchedResultsController sections] count]? :1;你錯過了中間的參數。我想你實際上錯過了第一個,並讓他們處於錯誤的位置。 – Jim 2012-03-06 21:50:44

+1

它看起來很奇怪,但實際上是正確的:)這是一個簡短的手返回版本[[fetchedResultsController部分] count]> 0? [[fetchedResultsController sections] count]:1; – Pieter 2012-03-06 21:58:19

+0

我發現這條消息通常意味着在實際獲得消息進行刪除/刪除的方法中出現了問題。從你的文章看來,你實際上並沒有改變它的部分數量?也許發佈您的部分刪除代碼? – AlexTheMighty 2012-03-06 22:06:05

回答

10

我發現這個問題/解決方案:

我沒有這種情況的需要didChangeSection委託方法!

- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo 
      atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type { 
NSLog(@"didChangeSection"); 

    switch(type) { 
     case NSFetchedResultsChangeInsert: 
      [self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade]; 
      break; 

     case NSFetchedResultsChangeDelete: 
      [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade]; 
      break; 
    } 
} 
0

我遇到了同樣的問題。但是,只是實施控制器:didChangeSection:atIndex:forChangeType方法並沒有解決我的崩潰。我必須做的是在一個原子更新中進行多表視圖更新(刪除部分+將行從剛刪除的部分移至現有或新的部分)。我簡單地補充說:

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller 
{ 
    [self.tableView beginUpdates]; 
} 

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller 
{ 
    [self.tableView endUpdates]; 
} 
相關問題