2012-03-06 76 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]; 
} 
相关问题