【问题标题】:How to properly delete rows in UITableView (controlled by NSFetchedResultsController) while also utilizing UISearchDisplayController?如何在使用 UISearchDisplayController 的同时正确删除 UITableView 中的行(由 NSFetchedResultsController 控制)?
【发布时间】:2011-05-05 09:50:19
【问题描述】:

在我的 iPhone 应用程序中,我有一个(未分组的)UITableView,它使用所有“花里胡哨”,包括 (a) NSFetchedResultsController 以保持更新,(b) 多个部分,(c) 添加并删除项目,以及 (d) 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)

问题似乎是由于当应用程序切换到搜索模式时,而不是通常的部分数量(按字母顺序,A-Z),它切换到单个部分(“搜索结果”)。因此,当它试图删除该项目时,它认为正确的部分数量是当它只是在单个部分(搜索结果)中时更大的数字。

这是我用于管理获取结果控制器的代码。 您知道处理此类操作的正确方法是什么吗?

- (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];
// }
}

【问题讨论】:

    标签: iphone uitableview uisearchdisplaycontroller


    【解决方案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];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-05
      相关资源
      最近更新 更多