i am trying to add search function in my app using NSpredicate
. i have tried to get this to work and can't seem to see where the issue is. i recreated a demo app to show what i have done. everything shows up on simulator without any warnings, but when typing a text in search bar, there is no results. nothing shows in the search bar tableview. i am using search bar and search display controller object and also my original data source is a simple plist that contains dictionaries and each dictionary has 3 strings. everything works just fine except the search. can someone please take a look at my code and see where i went wrong ?
here is my .h file:
#import <UIKit/UIKit.h>
@interface TableViewController : UITableViewController <UISearchBarDelegate>
@property (strong, nonatomic) NSArray *content;
@property (strong, nonatomic) NSMutableArray *searchResults;
@end
and here is the .m file:
#import "TableViewController.h"
#import "DetailViewController.h"
@interface TableViewController ()
@end
@implementation TableViewController
@synthesize content = _content;
@synthesize searchResults = _searchResults;
-(NSArray *)content
{
if (!_content) {
_content = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"]];
}
return _content;
}
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
_searchResults = [[NSMutableArray alloc] init];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == self.searchDisplayController.searchResultsTableView) {
return [self.searchResults count];
} else {
return [self.content count];
}
}
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
[_searchResults removeAllObjects];
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"SELF contains[cd] '%@'",searchText];
[_searchResults addObjectsFromArray:[_content filteredArrayUsingPredicate:resultPredicate]];
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
[self filterContentForSearchText:searchString
scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
objectAtIndex:[self.searchDisplayController.searchBar
selectedScopeButtonIndex]]];
return YES;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (tableView == self.searchDisplayController.searchResultsTableView) {
cell.textLabel.text = [_searchResults objectAtIndex:indexPath.row];
cell.detailTextLabel.text = [_searchResults objectAtIndex:indexPath.row];
} else {
cell.textLabel.text = [[self.content objectAtIndex:indexPath.row] valueForKey:@"city"];
cell.detailTextLabel.text = [[self.content objectAtIndex:indexPath.row] valueForKey:@"state"];
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView == self.searchDisplayController.searchResultsTableView) {
[self performSegueWithIdentifier: @"showDetails" sender: self];
}
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"showDetails"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
DetailViewController *DVC = [segue destinationViewController];
if ([self.searchDisplayController isActive]) {
DVC.cityImageString = [_searchResults objectAtIndex:indexPath.row];
DVC.cityTextString = [_searchResults objectAtIndex:indexPath.row];
} else {
DVC.cityImageString = [[self.content objectAtIndex:indexPath.row] valueForKey:@"cityImage"];
DVC.cityTextString = [[self.content objectAtIndex:indexPath.row] valueForKey:@"cityText"];
}
}
}
@end
p.s. i am using xcode 4.6 ios 6 which i don't think would matter and may not be related to my question and also, I have added the searchbar through IB and its delegate and data source were connected automatically to the tableview.
thanks a bunch for helping in advance.
Edit:
here is the structure of my plist which is a very simple one, and i thought it might help someone offering me an answer.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
<dict>
<key>city</key>
<string>New York</string>
<key>state</key>
<string>NY</string>
<key>cityText</key>
<string>This is the description of the city that goes in the tex view just testing.</string>
<key>cityImage</key>
<string>acolon.png</string>
</dict>
<dict>
<key>city</key>
<string>Los Angeles</string>
<key>state</key>
<string>CA</string>
<key>cityText</key>
<string>This the second item's textview</string>
<key>cityImage</key>
<string>Piedirosso.jpg</string>
</dict>
<dict>
<key>city</key>
<string>Chicago</string>
<key>state</key>
<string>IL</string>
<key>cityText</key>
<string>here is the text view description for the third item.</string>
<key>cityImage</key>
<string>acolon.png</string>
</dict>
</array>
</plist>
Your problem is the predicate on this line
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"SELF contains[cd] '%@'",searchText];
Your _content array contains NSDictionaries which don't support contains operator. Also, single/double quotes cause %@ to be used literally. (From docs: Single or double quoting variables (or substitution variable strings) cause %@, %K, or $variable to be interpreted as a literal in the format string and so prevent any substitution
). Change this to:
NSPredicate *pred = [NSPredicate predicateWithFormat: @"SELF['city'] contains[cd] %@ OR SELF['state'] contains[cd] %@ OR SELF['cityText'] contains[cd] %@", searchText, searchText, searchText];
(assuming you want to search all three values: city, state, cityText)