I have a new app for IOS where I'm parsing JSON and I'm using Search Bar. Parsing JSON works correctly, but I have problems with Search Bar, namely when I build my app and I want to search some words which were parsed I see nothing. Below I put my part of a code. I think a problem is with predicate :" predicateWithFormat:@"productName like[cd] %@", searchText];" Can anyone help me?
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];
}
}
}
This issue is most likely with the predicate setup, as you suspect.
I'm using something like this for searching for matches in a list of contacts, perhaps it's a viable alternative:
[NSPredicate predicateWithBlock:^BOOL(Contact *contact, NSDictionary *bindings){
return [contact.name rangeOfString:searchTerm options:NSCaseInsensitiveSearch].location != NSNotFound;
}];