Reputation: 103
I have created a searchbar following a tutorial here: http://www.appcoda.com/how-to-add-search-bar-uitableview/
However my search results are not returning anything. Only thing I can think of is that the NSArray
is not mutable however when changing that the resultPredicate gives me an error of incompatible pointer types.
.h
@interface plistTableViewController : UITableViewController<UISearchBarDelegate, UITableViewDataSource, UITableViewDelegate>
{
NSArray *tabledata;
NSArray *searchResults;
}
@end
.m
#import "plistTableViewController.h"
#import "DetailViewController.h"
@interface plistTableViewController ()
@end
@implementation plistTableViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
NSString *mylist = [[NSBundle mainBundle] pathForResource:@"lodges" ofType:@"plist"];
tabledata = [[NSArray alloc]initWithContentsOfFile:mylist];
NSLog(@"%@", tabledata);
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == self.searchDisplayController.searchResultsTableView) {
return [searchResults count];
} else {
return [tabledata count];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if( cell == nil ) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Cell"];
}
if (tableView == self.searchDisplayController.searchResultsTableView) {
cell.textLabel.text = [searchResults objectAtIndex:indexPath.row];
cell.detailTextLabel.text = [searchResults objectAtIndex:indexPath.row];
} else {
cell.textLabel.text = [[tabledata objectAtIndex:indexPath.row]objectForKey:@"cellName"];
cell.detailTextLabel.text = [[tabledata objectAtIndex:indexPath.row]objectForKey:@"cellSubtitle"];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
return cell;
}
// search bar code
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"SELF contains[cd] %@",searchText];
searchResults = [tabledata filteredArrayUsingPredicate:resultPredicate];
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller
shouldReloadTableForSearchString:(NSString *)searchString
{
[self filterContentForSearchText:searchString
scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
objectAtIndex:[self.searchDisplayController.searchBar
selectedScopeButtonIndex]]];
return YES;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView == self.searchDisplayController.searchResultsTableView) {
[self performSegueWithIdentifier: @"lodgedetail" sender: self];
}
}
// end of searchbar code
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSIndexPath *indexPath = [[self tableView] indexPathForSelectedRow];
NSDictionary *contactInfo = tabledata[indexPath.row];
DetailViewController *dvc = (DetailViewController *)[segue destinationViewController];
dvc.contactInfo = contactInfo;
if ([segue.identifier isEqualToString:@"lodgedetail"]) {
DetailViewController *dvc = segue.destinationViewController;
NSIndexPath *indexPath = nil;
if ([self.searchDisplayController isActive]) {
indexPath = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow];
dvc.contactInfo = [searchResults objectAtIndex:indexPath.row];
} else {
indexPath = [self.tableView indexPathForSelectedRow];
dvc.contactInfo = [tabledata objectAtIndex:indexPath.row];
}
}
}
@end
Upvotes: 0
Views: 672
Reputation: 26
It needs to have single quotes around the %@
otherwise you are comparing based on an object, not a string.
Upvotes: 1
Reputation: 103
// searchbar code
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"cellName contains[cd] '%@'",searchText]];
NSLog(@"%@", self.tabledata);
self.searchResults = [NSMutableArray arrayWithArray:[self.tabledata filteredArrayUsingPredicate:resultPredicate]];
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller
shouldReloadTableForSearchString:(NSString *)searchString
{
[self filterContentForSearchText:searchString
scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
objectAtIndex:[self.searchDisplayController.searchBar
selectedScopeButtonIndex]]];
return YES;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView == self.searchDisplayController.searchResultsTableView) {
[self performSegueWithIdentifier: @"lodgedetail" sender: self];
}
}
// end of searchbar code
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSIndexPath *indexPath = [[self tableView] indexPathForSelectedRow];
NSDictionary *contactInfo = tabledata[indexPath.row];
DetailViewController *dvc = (DetailViewController *)[segue destinationViewController];
dvc.contactInfo = contactInfo;
if ([segue.identifier isEqualToString:@"lodgedetail"]) {
DetailViewController *dvc = segue.destinationViewController;
NSIndexPath *indexPath = nil;
if ([self.searchDisplayController isActive]) {
indexPath = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow];
dvc.contactInfo = [searchResults objectAtIndex:indexPath.row];
} else {
indexPath = [self.tableView indexPathForSelectedRow];
dvc.contactInfo = [tabledata objectAtIndex:indexPath.row];
}
}
}
@end
Upvotes: 0
Reputation: 2122
Then, suppose if you are searching based on the cellName
, then your NSPredicate
should be like this:
[searchResults removeAllObjects];
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"cellName contains[cd] %@",searchText];
[searchResults addObjectsFromArray:[tabledata filteredArrayUsingPredicate:resultPredicate]];
Try this out, your problem will be solved...
Upvotes: 1