Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Replace SearchDisplayController (Deprecated in IOS 8) by UIsearchController method

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];

        }
    }

}
like image 447
Daniel Humfy Avatar asked Dec 18 '14 19:12

Daniel Humfy


2 Answers

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

like image 20
Pushp Avatar answered Nov 14 '22 04:11

Pushp


I know this post is old but I was facing the same issue and I have found the following tutorials, much easier than Apple's example:

  1. http://www.ioscreator.com/tutorials/add-search-table-view-tutorial-ios8-swift
  2. http://useyourloaf.com/blog/2015/02/16/updating-to-the-ios-8-search-controller.html

Choose what is more suitable for your need and have a happy coding!

Cheers,

like image 176
Ziv Levy Avatar answered Nov 14 '22 04:11

Ziv Levy