I'm obviously still new to Xcode. So SeacrhDisplayController
is deprecated in iOS 8 and i don't know how to implement UIsearchController
on tableview.
I have googled about it, but i don't see any particular or clear tutorials for this.
Here is my code for SearchDisplayController
:
-(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 [categories count];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"FirstTableCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
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 = [categories objectAtIndex:indexPath.row];
}
cell.imageView.image = [UIImage imageNamed:[thumbnails objectAtIndex:indexPath.row]];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView == self.searchDisplayController.searchResultsTableView) {
[self performSegueWithIdentifier: @"showArrayDetail" sender: self];
}
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"showArrayDetail"]) {
SecondViewController *destViewController = segue.destinationViewController;
NSIndexPath *indexPath = nil;
if ([self.searchDisplayController isActive]) {
indexPath = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow];
destViewController.categoryName = [searchResults objectAtIndex:indexPath.row];
} else {
indexPath = [self.tableView1 indexPathForSelectedRow];
destViewController.categoryName = [categories objectAtIndex:indexPath.row];
}
}
}
I have the same issue i follow this steps:
Step 1:
Add the UISearchResultsUpdating
at your .h file
Step 2:
Create Property of UISearchController
@property(nonatomic, strong) IBOutlet UISearchController *searchController
Step 3: Add the code at viewDidLoad()
- (void)viewDidLoad
{
NSLog(@"viewDidLoad Starts");
[super viewDidLoad];
self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater = self;
self.searchController.dimsBackgroundDuringPresentation = false;
self.tableView.tableHeaderView = self.searchController.searchBar;
}
Step 4:
Impliment your Filter Stuff at updateSearchResultsForSearchController
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
// do your Filter code and reload tableView
[self.tableView reloadData];
}
Step 5: change your codition at numberOfRowsInSection
if (self.searchController.isActive) {
return [searchResults count];
} else {
return [categories count];
}
Step 6: Now just run and check