2012-02-06 107 views
0

使用核心数据,数据正在被正确提取并正确显示,问题在于搜索不会过滤搜索结果,无论我在搜索栏中输入什么,它都会显示相同的表格视图在筛选出的结果相同的数据..核心数据搜索问题iOS 5

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    if (context == nil) 
    { 
     context = [(VektorAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext]; 
    }  
    app = [[UIApplication sharedApplication] delegate]; 

    [self getData]; 

    // create a filtered list that will contain products for the search results table. 

    filteredListItems = [[NSMutableArray alloc] initWithCapacity:[self.plateNumbers count]]; 

    // restore search settings if they were saved in didReceiveMemoryWarning. 
    if (self.savedSearchTerm){ 
     [self.searchDisplayController setActive:self.searchWasActive]; 
     [self.searchDisplayController.searchBar setSelectedScopeButtonIndex:self.savedScopeButtonIndex]; 
     [self.searchDisplayController.searchBar setText:savedSearchTerm]; 

    self.savedSearchTerm = nil; 
    } 
} 

撷取来自核心数据的数据:

-(void)getData { 

NSEntityDescription *entity = [NSEntityDescription entityForName:@"Favouritesdata" inManagedObjectContext:context]; 

NSFetchRequest *request = [[NSFetchRequest alloc] init]; 

[request setFetchBatchSize:20]; 

[request setEntity:entity]; 

NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"licenseplate" ascending:NO]; 

NSArray *newArray = [[NSArray alloc]initWithObjects:sort, nil]; 

[request setSortDescriptors:newArray]; 

NSLog(@"newArray: %@", newArray); 

NSError *error; 

results = [[context executeFetchRequest:request error:&error] mutableCopy]; 

plateNumbers = [results valueForKey:@"licenseplate"]; 

NSLog(@"plateNumbers: %@", plateNumbers); 

[self setLicensePlateArray:results]; 

[self.favouritesTable reloadData]; 

} 

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
if (tableView == favouritesTable) { 
    return [licensePlateArray count]; 
} else { // handle search results table view 
    return [filteredListItems count]; 
    }  
} 

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

if (tableView == favouritesTable) { 
    cellValue = [licensePlateArray objectAtIndex:indexPath.row]; 
} else { // handle search results table view 
    cellValue = [filteredListItems objectAtIndex:indexPath.row]; 
} 

static NSString *CellIdentifier = @"vlCell"; 

VehicleListCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

if (cell == nil) { 

    NSLog(@"Cell Created"); 

    NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:@"VehicleListCell" owner:nil options:nil]; 

    for (id currentObject in nibObjects) { 
     if ([currentObject isKindOfClass:[VehicleListCell class]]) { 
      cell = (VehicleListCell *)currentObject; 
     } 
    } 
    NSInteger cellVal = indexPath.row; 

    NSLog(@"indexPath.row: %i", cellVal); 

    UILongPressGestureRecognizer *pressRecongnizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(tableCellPressed:)]; 
    pressRecongnizer.delegate = self; 
    pressRecongnizer.minimumPressDuration = 0.5f; 
    [cell addGestureRecognizer:pressRecongnizer]; 
    [pressRecongnizer release]; 
} 

cell.textLabel.font = [UIFont systemFontOfSize:10]; 

Favouritesdata *favdata = [results objectAtIndex:indexPath.row]; 

cell.licPlate.text = [favdata licenseplate]; 

NSLog(@"cellvalue for cellforRow: %@", cell.licPlate.text); 

return cell; 
} 

搜索栏实现:

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope 
{  
    NSPredicate *resultPredicate = [NSPredicate 
            predicateWithFormat:@"SELF contains[cd] %@", 
            searchText]; 

    self.filteredListItems = [self.plateNumbers filteredArrayUsingPredicate:resultPredicate]; 

} 

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString { 
    /* [self filterContentForSearchText:searchString scope: 
    [[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]]; 
    */ 

    if ([[searchString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length]) 
     self.favouritesTable = controller.searchResultsTableView; 

    return YES; 
} 

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption { 
    [self filterContentForSearchText:[self.searchDisplayController.searchBar text] scope: 
    [[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:searchOption]]; 
    return YES; 
} 

如何解决此问题?

+0

你缺少为你设置一个NSPredicate请求 – CarlJ 2012-02-06 12:29:01

+0

如何以及在哪里设置它? – 2012-02-06 12:30:47

+0

https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreData/Articles/cdFetching.html#//apple_ref/doc/uid/TP40002484-SW1 – CarlJ 2012-02-06 12:32:30

回答

0

只要在代码清晰并定义每个方法之前,您只需设置谓词,而不是为其启动查询。

只是做出单独的方法,只是将搜索文本作为参数传递给该方法以进行谓词和取消查询,然后重新载入表。

+0

但我没有得到设置在哪里谓词,在filterContentForSearchText中:已经设置了一个谓词。 – 2012-02-06 13:46:17