I'm practicing on UISearch bar. It's supposed to show what it scopes, however I doesn't work. I connected search bar view controller to the original view controller. I checked the console (I put the NSLog to see if the NSPredicate works property), and it says the result, but not on the screen. Any advice please. Thanks:)
#import "ViewController.h" #import "detailViewController.h" // I realized it's not good practice. The first letter shold be the upper case
@interface ViewController ()
{
NSMutableArray *_recipes;
NSString *_recipeName;
NSArray *_searchResults;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.tableView.delegate = self;
self.tableView.dataSource = self;
_recipes = [NSMutableArray arrayWithObjects:@"Egg Benedict",@"Mushroom Risotto",@"Full Breakfast", @"Hamburger",@"Ham and Egg Sandwich", @"Cream Brelee",@"White Chocolate Donut",@"Starbucks Coffee",@"Vegetable Curry", @"Instant Noodle With Egg",@"Noodle with BBQ Pork", @"Japanese Noodle with Pork",@"Green Tea", @"Thai shripm Cake", @"Angry Birds Cake",@"Ham and Cheese Panini",nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
# pragma mark Dlegate method
// Data source protocol
- (NSInteger)tableView:(UITableView *)tableview numberOfRowsInSection:(NSInteger)section
{
if(self.tableView == self.searchDisplayController.searchResultsTableView)
{
return [_searchResults count];
}
else
{
return [_recipes count];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *simpleTableIdentifier = @"RecipeCell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
if(self.tableView == self.searchDisplayController.searchResultsTableView)
{
cell.textLabel.text = _searchResults[indexPath.row];
}
else
{
cell.textLabel.text = _recipes[indexPath.row];
}
return cell;
}
// Delegate Protocol
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"row %i and section %i",indexPath.row,indexPath.section);
_recipeName = _recipes[indexPath.row];
[self performSegueWithIdentifier:@"CellSelectionSegue" sender:self];
}
# pragma mark Segue method
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSLog(@"segue");
detailViewController *detailVC = segue.destinationViewController;
detailVC.string = _recipeName;
NSLog(@"%@",_recipeName);
}
# pragma mark Searchbar datasource
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
NSPredicate *resultPredicate = [NSPredicate
predicateWithFormat:@"SELF contains[cd] %@",
searchText];
_searchResults = (NSArray *)[_recipes filteredArrayUsingPredicate:resultPredicate];
NSLog(@"%@",_searchResults);
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller
shouldReloadTableForSearchString:(NSString *)searchString
{
[self filterContentForSearchText:searchString
scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
objectAtIndex:[self.searchDisplayController.searchBar
selectedScopeButtonIndex]]];
return YES;
}
@end
Have you set the delegates for the SearchDisplayController
?
self.searchDisplayController.searchResultsDelegate = self;
self.searchDisplayController.searchResultsDataSource = self;
self.searchDisplayController.delegate = self;
Edit:
I'll edit my comment into here. I believe the references for
if (self.tableView == self.searchDisplayController.searchResultsTableView)
should be
if (tableView == self.searchDisplayController.searchResultsTableView)
The reason is self.tableView
will never be the SearchDisplayController
because when initiating the search it actually creates a new UITableViewController
on top of the original.
There is one more delegate method in search
. Try adding that to your project. Here is the code:
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption
{
[self filterContentForSearchText:self.searchDisplayController.searchBar.text
scope:[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:searchOption]];
return YES;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.