使用带有核心数据的搜索栏

时间:2011-10-25 01:49:11

标签: iphone uitableview core-data uisearchbar

我尝试了各种各样的方法,这是我得到的最接近的方法,但是表格的更新使屏幕上的所有条目都空白,当我滚动时,它们全都空白。我还是新手,并不完全确定在核心数据tableview上实现搜索。

如果我不解决问题就会死在这里:

更新:这就是我现在正在尝试的,而且我遇到了同样的崩溃: 的 Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'no section at index 1'

`Recipe *recipe = (Recipe *)[fetchedResultsController objectAtIndexPath:indexPath];` **`Thread 1 received signal SIGABRT`**


#pragma mark -
#pragma mark search bar methods
- (void) searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    NSLog(@"searched");
    fetchedResultsController = nil;       
    //============
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Recipe" inManagedObjectContext:managedObjectContext];

    if (searchBar.text !=nil) {
        NSPredicate *predicate =[NSPredicate predicateWithFormat:@"name contains[cd] %@", searchBar.text];
        [fetchedResultsController.fetchRequest setPredicate:predicate];
    } else {
        NSPredicate *predicate =[NSPredicate predicateWithFormat:@"All"];
        [fetchedResultsController.fetchRequest setPredicate:predicate];
    }

    [fetchRequest setEntity:entity];

    // Edit the sort key as appropriate.
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"state" ascending:YES];// was name
    NSSortDescriptor *sortDescriptor2 = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];

    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor,sortDescriptor2, nil];// was 2

    [fetchRequest setSortDescriptors:sortDescriptors];

    // Edit the section name key path and cache name if appropriate.
    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:@"state" cacheName:@"Root"];//@"state"
    aFetchedResultsController.delegate = self;
    self.fetchedResultsController = aFetchedResultsController;

    [aFetchedResultsController release];
    [fetchRequest release];
    [sortDescriptor release];
    [sortDescriptor2 release];
    [sortDescriptors release];
    //==============

    // dismiss the search keyboard
    [searchBar resignFirstResponder];

    // reload the table view
    //[self.tableView reloadData];

}


- (void)configureCell:(RecipeTableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
    // Configure the cell //DIES HERE
    Recipe *recipe = (Recipe *)[fetchedResultsController objectAtIndexPath:indexPath];
    cell.recipe = recipe;
}

在其他情况下,当我尝试向上或向下移动桌子时它会死亡。

感谢您对理解和解决此问题的任何指导或帮助!

罗布

tableViewController.m

- (NSFetchedResultsController *)fetchedResultsController {
    if (fetchedResultsController == nil) {
        NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
        NSEntityDescription *entity = [NSEntityDescription entityForName:@"Recipe" inManagedObjectContext:managedObjectContext];
        [fetchRequest setEntity:entity];

        // Edit the sort key as appropriate.
        NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"state" ascending:YES];// was name
        NSSortDescriptor *sortDescriptor2 = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];

        NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor,sortDescriptor2, nil];// was 2

        [fetchRequest setSortDescriptors:sortDescriptors];

        // Edit the section name key path and cache name if appropriate.
        NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:@"state" cacheName:@"Root"];//@"state"
        aFetchedResultsController.delegate = self;
        self.fetchedResultsController = aFetchedResultsController;

        [aFetchedResultsController release];
        [fetchRequest release];
        [sortDescriptor release];
        [sortDescriptor2 release];
        [sortDescriptors release];
    }
    return fetchedResultsController;
} 

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
    self.fetchedResultsController = nil;

    self.searchBar.text=@"";

    [self.searchBar setShowsCancelButton:NO animated:YES];
    [self.searchBar resignFirstResponder];
    //self.tableView.allowsSelection = YES;
    //self.tableView.scrollEnabled = YES;
}
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
    // added 2 below
    //self.fetchedResultsController.delegate = nil;
    self.fetchedResultsController = nil;
    [self.searchBar setShowsCancelButton:YES animated:YES];
    //self.tableView.allowsSelection = NO;
    //self.tableView.scrollEnabled = NO;
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    // added 2 below
    //self.fetchedResultsController.delegate = nil;
    self.fetchedResultsController = nil;
    NSLog(@"fetchObjects");
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Recipe" inManagedObjectContext:managedObjectContext];
    [fetchRequest setEntity:entity];

    // Edit the sort key as appropriate.
    //NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"state" ascending:YES];// was name
    NSSortDescriptor *sortDescriptor2 = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];

    //NSLog(@"NSInteger value :%@", sortDescriptor);
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor2, nil];

    [fetchRequest setSortDescriptors:sortDescriptors];

    // perform query
    NSString *query = self.searchBar.text;
    if (query && query.length) fetchRequest.predicate = [NSPredicate predicateWithFormat:@"name CONTAINS[cd] %@", query];


    // Edit the section name key path and cache name if appropriate.
    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:@"state" cacheName:@"Root"];//@"state"
    aFetchedResultsController.delegate = self;
    self.fetchedResultsController = aFetchedResultsController;

    [aFetchedResultsController release];
    [fetchRequest release];
    //[sortDescriptor release];
    [sortDescriptor2 release];
    [sortDescriptors release];
    //[self.tableView reloadData];
}   

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
    // The fetch controller is about to start sending change notifications, so prepare the table view for updates.
    [self.tableView beginUpdates];
}

2 个答案:

答案 0 :(得分:1)

看起来你实际上并没有真正执行获取请求。来自docs

  

创建实例后,实际调用performFetch:   执行获取。

答案 1 :(得分:1)

您使用的是UISearchDisplayController吗?如果你最终得到两个表:一个显示你的正常东西,另一个与搜索相关。您必须单独处理表格。

How to filter NSFetchedResultsController (CoreData) with UISearchDisplayController/UISearchBar

的表格上实现搜索所需的所有代码的书面记录