用核心数据iPhone实现搜索

时间:2011-11-12 22:20:13

标签: iphone search core-data nsfetchedresultscontroller

我想添加在我的核心数据库中搜索的功能 我有一个带有自定义单元格的表格视图但它们只有8并且它们不可编辑 现在,我向searchView的headerView添加了一个带有searchDisplayController的searchBar,我想使用NSFetchedResultsController来管理搜索数据。
我试着编写这段代码,但它不起作用......

-(void)createSearch
{
    UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    searchBar.delegate = self;
    searchBar.scopeButtonTitles = [NSArray arrayWithObjects:@"All", @"Title", @"Notes", @"Tags", nil];
    searchBar.showsScopeBar = YES;
    self.tableView.tableHeaderView = searchBar;
    [searchBar setBackgroundImage:[UIImage imageNamed:@"navbarBg"]];

    searchController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
    searchController.delegate = self;
    searchController.searchResultsDataSource = self;
    searchController.searchResultsDelegate = self;
}

-(NSFetchedResultsController *)searchResultsController
{
    if (searchResultsController != nil)
    {
        return searchResultsController;
    }

    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Task"];
    [request setFetchBatchSize:20];

    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"title" ascending:YES];
    NSArray *descriptors = [NSArray arrayWithObject:sortDescriptor];
    [request setSortDescriptors:descriptors];

    NSFetchedResultsController *afrc = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:nil];
    afrc.delegate = self;
    searchResultsController = afrc;

    return searchResultsController;
}

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

-(void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath
{
    UITableView *tableView = self.searchController.searchResultsTableView;

    switch(type) 
    {
        case NSFetchedResultsChangeInsert:
            [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;

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

        case NSFetchedResultsChangeUpdate:
            [self configureCell:[self tableView:tableView cellForRowAtIndexPath:indexPath] toIndexPath:indexPath];
            break;

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

-(void)controller:(NSFetchedResultsController *)controller didChangeSection:(id<NSFetchedResultsSectionInfo>)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type
{
    UITableView *tableView = self.searchController.searchResultsTableView;

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

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

-(void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
    [self.searchController.searchResultsTableView endUpdates];
}

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    NSPredicate *predicate = nil;

    if (self.searchController.searchBar.selectedScopeButtonIndex == 0)
    {
        predicate = [NSPredicate predicateWithFormat:@"1=1"];
    }
    else if (self.searchController.searchBar.selectedScopeButtonIndex == 1)
    {
        predicate = [NSPredicate predicateWithFormat:@"title CONTAINS[c] %@", searchText];
    }
    else if (self.searchController.searchBar.selectedScopeButtonIndex == 2)
    {
        predicate = [NSPredicate predicateWithFormat:@"notes CONTAINS[c] %@", searchText];
    }
    /*else
    {
        predicate = [NSPredicate predicateWithFormat:@"tags
    }*/

    [self.searchResultsController.fetchRequest setPredicate:predicate];

    [self.searchController.searchResultsTableView reloadData];
}

这些是tableView委托和dataSource方法:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (tableView == self.searchController.searchResultsTableView)
    {
        return [[self.searchResultsController sections] count];
    }
    else
    {
        return 1;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.searchController.searchResultsTableView)
    {
        id <NSFetchedResultsSectionInfo> sectionInfo = [[self.searchResultsController sections] objectAtIndex:section];
        return [sectionInfo numberOfObjects];
    }
    else
    {
        return 8;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.searchController.searchResultsTableView)
    {
        static NSString *SearchCellIdentifier = @"SearchCell";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SearchCellIdentifier];
        if (cell == nil)
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SearchCellIdentifier];
        }

        [self configureCell:cell toIndexPath:indexPath];

        return cell;
    }
    else
    {
        static NSString *CellIdentifier = @"Cell";

        MainTableCell *cell = (MainTableCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[MainTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
            cell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"accessoryView"]];
        }

        [self configureCell:cell fromArray:cellImages toIndexPath:indexPath];

        return cell;
    }
}

为什么不起作用?

非常感谢你;)

1 个答案:

答案 0 :(得分:0)

在您调用reloadData之前,在您的searchBar:textDidChange:消息的末尾

[searchResultsController release]; searchResultsController = nil;