How to show searchResultsTableView when search bar is activated?

I implement UISearchDisplayController , and I would like to populate searchResultsTableView contents of the View table (unfiltered) on the right when loading - before entering text.

This works when I start typing in a searchBar.

 - (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller { self.isSearching = YES; //tell the searchDisplayTableView to show here! [controller.searchBar setShowsCancelButton:YES animated:YES]; } - (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller { self.isSearching = NO; [controller.searchBar setShowsCancelButton:NO animated:YES]; } 

Does anyone have pointers in the right direction?

Please do not answer "do not do this" or "Apple has not created control in this way." or...

+6
source share
4 answers

Apple management works just that way. I solved this by implementing a separate controller (calling its display controller), which is a delegate to the search bar and applies the predicate to the data source. Thus, the tableview is filtered "inline".

0
source

You need to reload the table view with the filtered result when performing a search: In this method, just call the table view reloadData :

 - (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller { self.isSearching = YES; [yourTableView reloadData]; [controller.searchBar setShowsCancelButton:YES animated:YES]; } 

And change your numberOfSectionsInTableView method as follows:

 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { if(isSearching == YES) { yourDataArray = //filtered result; } else { yourDataAray = //unfiltered result; } return 1; } 

Hope this helps you.

0
source
 - (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller { // Here is a table we want to show in UISearchDisplayController XXRecentSearchDataSource *recentSearch = [XXRecentSearchDataSource recentSearchDataSource]; recentSearch.delegate = self; // Setup default table view CGRect frame = CGRectMake(0.0f, CGRectGetMaxY(controller.searchBar.frame), CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds) - CGRectGetHeight(controller.searchBar.bounds)); // Setup temporary table and remember it for future use [_tempRecentSearchTableView release]; _tempRecentSearchTableView = [[UITableView alloc] initWithFrame:frame style:UITableViewStylePlain]; _tempRecentSearchTableView.dataSource = recentSearch; _tempRecentSearchTableView.delegate = recentSearch; [self performSelector:@selector(removeOverlay) withObject:nil afterDelay:.0f]; } - (void)removeOverlay { [[self.view.subviews lastObject] removeFromSuperview]; [self.view addSubview:_tempRecentSearchTableView]; } 

In some cases, you must remove _tempRecentSearchTableView. For instance:

 - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText { // Remove temporary table view [_tempRecentSearchTableView removeFromSuperview]; } 
0
source

you know there’s a focus in the search bar.

 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if ([_searchBar isFirstResponder]) { return [_filteredData count]; } else { id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section]; return [sectionInfo numberOfObjects]; } } 

When setting up a cell, ask the same question

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"CustomCell"; CustomTableViewCell *cell = (CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; // Configure the cell... if (cell == nil) { cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } Entidad *e = nil; if ([_searchBar isFirstResponder]) { e = [self.filteredData objectAtIndex:indexPath.row]; } else { e = [self.fetchedResultsController objectAtIndexPath:indexPath]; } cell.nameLabel.text = e.titulo; cell.thumbnailImageView.image = [UIImage imageNamed:@"password-50.png"]; cell.dateLabel.text = e.sitio; return cell; } 

Set filter options

 -(void)filter:(NSString*)text { _filteredData = [[NSMutableArray alloc] init]; NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; NSEntityDescription *entity = [NSEntityDescription entityForName:@"Entidad" inManagedObjectContext:self.managedObjectContext]; [fetchRequest setEntity:entity]; NSSortDescriptor* sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"titulo" ascending:YES]; NSArray* sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil]; [fetchRequest setSortDescriptors:sortDescriptors]; if(text.length > 0) { NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(titulo CONTAINS[c] %@)", text]; [fetchRequest setPredicate:predicate]; } NSError *error; NSArray* loadedEntities = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error]; _filteredData = [[NSMutableArray alloc] initWithArray:loadedEntities]; NSLog(@"%@", _filteredData); [self.tableView reloadData]; } -(void)searchBar:(UISearchBar*)searchBar textDidChange:(NSString*)text { [self filter:text]; } -(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{ [self filter:@""]; [_searchBar resignFirstResponder]; [_searchBar setText:@""]; [_tableView reloadData]; } - (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar { [_searchBar setShowsCancelButton:YES animated:YES]; } -(void)searchBarTextDidEndEditing:(UISearchBar *)searchBar { [_searchBar setShowsCancelButton:NO animated:YES]; } 

I hope to serve you, my English is very limited.

0
source

Source: https://habr.com/ru/post/921552/


All Articles