2

With iOS 8, UISearchDisplayController was deprecated, leaving UISearchController the option for adding search behavior to the applications. This forces the developers to create a separate UITableviewController to display the search results – lets call this tableViewController for resultsTableViewController. How to migrate from UISearchDisplayController to UISearchController is well covered in this thread: searchDisplayController deprecated in iOS 8

In my applications, I am using interfacebuilder and storyboard. My UITableViewController is customized in the storyboard, with custom cells, giving me what i want. However, I want my resultsTableViewController to look exactly like my tableViewController.

Question: Is there a way to use the same scene in the storyboard for both my tableViewController and resultsTableViewController? If not, what is the best workaround? This question is partly answered here: How to share one storyboard scene between multiple UIViewControllers, but I still have a way to go.

Community
  • 1
  • 1
Tom Tallak Solbu
  • 561
  • 6
  • 19

1 Answers1

2

With help from tartempion in the Apple developer forum, I finally managed a UTableviewController and searchViewController in one file, enabling me to use the same storyboard scene to display search results.

@interface TableViewController () <UISearchBarDelegate, UISearchControllerDelegate, UISearchResultsUpdating>
{
    UISearchController *_searchcontroller;

     NSArray * myArray;
     NSArray * myDisplayedArray; 
}
@end

@implementation TableViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    [...]

     _searchcontroller=[[UISearchController alloc]initWithSearchResultsController:nil];
     _searchcontroller.searchResultsUpdater =self;
     _searchcontroller.dimsBackgroundDuringPresentation=NO;

     myDisplayedArray=myArray;
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return [myDisplayedArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    MyClass*myInstance =[myDisplayedArray objectAtIndex:[indexPath row]];

  //and however you want to configure cells...

}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

  //whatever you need...
  //and to get rid of the searchcontroller when you move to the UIView that is connected
        self.searchcontroller.active=NO;
                 //or
        [self.searchcontroller dismissViewControllerAnimated:NO completion:nil];

}

#pragma mark -

- (void)updateSearchResultsForSearchController:(UISearchController *)inSearchController
{
    if (_searchController==inSearchController)
    {
        NSString *tSearchText = inSearchController.searchBar.text;

        if ([tSearchText length]==0)
        {
            myDisplayedArray=myArray;
        }
        else
        {
            myDisplayedArray=[myArray filteredWithPattern:tSearchText];
        }

        [self.tableView reloadData];
    }
}

@end
Tom Tallak Solbu
  • 561
  • 6
  • 19