2010-11-05 37 views
1

在我的iPhone应用程序,我有一个(非分组)的UITableView它使用所有的“花里胡哨”,包括(一)NSFetchedResultsController保持更新,(B)多个部分,(c)中添加的能力,删除项目,和(d)的UISearchDisplayController允许用户在列表中,可以得到很长的搜索。如何正确的UITableView(由NSFetchedResultsController控制)删除行同时还利用UISearchDisplayController?

我遇到问题,当用户在搜索过程中尝试删除项目时。我得到这个错误:Serious application error. An exception was caught from the delegate of NSFetchedResultsController during a call to -controllerDidChangeContent:. Invalid update: invalid number of sections. The number of sections contained in the table view after the update (1) must be equal to the number of sections contained in the table view before the update (21), plus or minus the number of sections inserted or deleted (0 inserted, 0 deleted). with userInfo (null)

看来,问题出现的原因是,当应用程序切换到搜索模式,而不是通常数量的部分(这是按字母顺序排列的AZ),它切换到单个部分(“搜索结果”)。所以,当它试图删除的项目,它被认为部分适当数量越大数时,它仅仅是在一节里(搜索结果)。

这里是我的管理获取的成果控制器代码。 你知道什么是处理这种类型的行动的正确方法?

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller { 
// if (!self.searchDisplayController.isActive) { 
    [self.tableView beginUpdates]; 
// } 
} 


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

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

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


- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject 
     atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type 
     newIndexPath:(NSIndexPath *)newIndexPath { 

    UITableView *tableView = self.tableView; 

    switch(type) { 

     case NSFetchedResultsChangeInsert: 
      [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade]; 
      break; 

     case NSFetchedResultsChangeDelete: 

    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
    break; 

     case NSFetchedResultsChangeUpdate: 
    if (!self.searchDisplayController.isActive) { 
    [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
    } else { 
    // I currently don't do anything if the search display controller is active, because it is throwing similar errors. 
    } 
    break; 

     case NSFetchedResultsChangeMove: 
      [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
      [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]withRowAnimation:UITableViewRowAnimationFade]; 
      break; 
    } 
} 


- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller { 
// if (!self.searchDisplayController.isActive) { 
    [self.tableView endUpdates]; 
// } 
} 

回答

1

如果你有一个NSFetchedResultsController,你为什么不只是创造一个谓语,并设置控制器作为过滤器上?

事情是这样的:

NSPredicate *predicate =[NSPredicate predicateWithFormat:@"name contains[cd] %@", searchText]; 
[fetchedResultsController.fetchRequest setPredicate:predicate]; 

NSError *error = nil; 
if (![[self fetchedResultsController] performFetch:&error]) { 
     // Handle error 
     NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
     // Fail 
}   

[self.tableView reloadData]; 
相关问题