我有一个用于IOS的新应用,其中正在解析JSON并且正在使用搜索栏。解析JSON可以正常工作,但是搜索栏存在问题,即在构建应用程序时,我想搜索一些已解析的单词时,什么也看不到。下面,我把我的代码部分。我认为谓词存在问题:“predicateWithFormat:@” productName like [cd]%@“,searchText];”谁能帮我?
tableViewController.m
-(void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"productName like[cd] %@", searchText];
searchResults = [productsArray filteredArrayUsingPredicate:predicate];
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller
shouldReloadTableForSearchString:(NSString *)searchString
{
[self filterContentForSearchText:searchString
scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
objectAtIndex:[self.searchDisplayController.searchBar
selectedScopeButtonIndex]]];
return YES;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == self.searchDisplayController.searchResultsTableView){
return [searchResults count];
} else {
return [productsArray count]; }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
Product *productObject;
productObject = [productsArray objectAtIndex:indexPath.row];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
if (tableView == self.searchDisplayController.searchResultsTableView)
{
cell.textLabel.text = [searchResults objectAtIndex:indexPath.row];
}
else
{
cell.textLabel.text = productObject.productName;
}
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView == self.searchDisplayController.searchResultsTableView) {
[self performSegueWithIdentifier: @"Cell" sender: self];
}
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"Cell"]) {
DetailViewController *destViewController = segue.destinationViewController;
if ([self.searchDisplayController isActive]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
indexPath = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow];
destViewController.imageView = [searchResults objectAtIndex:indexPath.row];
} else {
//indexPath = [self.tableView indexPathForSelectedRow];
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
Product * object =[productsArray objectAtIndex:indexPath.row];
[[segue destinationViewController] getProduct: object];
//destViewController.idLabel = [productsArray objectAtIndex:indexPath.row];
}
}
}
最佳答案
如您所怀疑,此问题最有可能与谓词设置有关。
我正在使用类似的方法在联系人列表中搜索匹配项,也许这是一个可行的选择:
[NSPredicate predicateWithBlock:^BOOL(Contact *contact, NSDictionary *bindings){
return [contact.name rangeOfString:searchTerm options:NSCaseInsensitiveSearch].location != NSNotFound;
}];
关于ios - 搜索栏xcode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25186157/