Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here is a sample code</p> <pre><code>NSMutableArray *filteredResult; // this holds filtered data source NSMutableArray *tableData; //this holds actual data source -(void) filterForSearchText:(NSString *) text scope:(NSString *) scope { [filteredResult removeAllObjects]; // clearing filter array NSPredicate *filterPredicate = [NSPredicate predicateWithFormat:@"SELF.restaurantName contains[c] %@",text]; // Creating filter condition filteredResult = [NSMutableArray arrayWithArray:[tableData filteredArrayUsingPredicate:filterPredicate]]; // filtering result } </code></pre> <h1>Delegate Methods</h1> <pre><code>-(BOOL) searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString { [self filterForSearchText:searchString scope:[[[[self searchDisplayController] searchBar] scopeButtonTitles] objectAtIndex:[[[self searchDisplayController] searchBar] selectedScopeButtonIndex] ]]; return YES; } -(BOOL) searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption { [self filterForSearchText:self.searchDisplayController.searchBar.text scope: [[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:searchOption]]; return YES; } </code></pre> <p>In NSPredicate condition "@"SELF.restaurantName contains[c] %@",text " restaurantName is a property name which needs to filtered against. If you have only NSString in your datasource array, you can use like @"SELF contains[c] %@",text</p> <p>Once the filter is done, then you need to implement your tableview delegate accordingly. Something like this</p> <pre><code>- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if(tableView == [[self searchDisplayController] searchResultsTableView]) { return [filteredResult count]; } else { return [tableData count]; } } </code></pre> <p>compare the tableview whether it is filtered tableview or original tableview and set the delegate and datasource for tableview accordingly.Please note, searchDisplayController is available property for UIViewcontroller and we can just use it to display filtered result.</p> <p>For above code to work, you need to use "Search Bar and Search Display" object if you are using it in a XIB or storyboard </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload