【问题标题】:core data search issue iOS 5核心数据搜索问题 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;
}

如何解决这个问题?

【问题讨论】:

标签: iphone objective-c ios


【解决方案1】:

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

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

【讨论】:

  • 但我不知道在哪里设置谓词,在 filterContentForSearchText:已经设置了一个谓词。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-17
  • 1970-01-01
  • 2014-05-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多