核心数据搜索问题 iOS 5

Posted

技术标签:

【中文标题】核心数据搜索问题 iOS 5【英文标题】:core data search issue iOS 5 【发布时间】:2012-02-06 12:23:08 【问题描述】:

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

- (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;

如何解决这个问题?

【问题讨论】:

您缺少为您的请求设置 NSPredicate developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/… 【参考方案1】:

之前只要明确代码并定义每个方法,你只是设置谓词,而不是为它触发查询。

只需创建单独的方法并将搜索文本作为参数传递给该方法以进行谓词并触发获取查询,然后重新加载表。

【讨论】:

但我不知道在哪里设置谓词,在 filterContentForSearchText:已经设置了一个谓词。

以上是关于核心数据搜索问题 iOS 5的主要内容,如果未能解决你的问题,请参考以下文章

iOS:Swift:核心数据:错误:+entityForName:nil 不是搜索实体名称的合法 NSManagedObjectContext 参数

iOS核心数据索引属性没有提高性能

使用带有 ios 5 核心数据并发的 RestKit 时崩溃

核心数据不在 iOS 5 中保留

在 iOS 5 上实现快速高效的核心数据导入

iOS 5 - 按核心数据属性将 UITableView 划分为多个部分