tutorial searchcontroller bar and ios uisearchdisplaycontroller

ios - searchcontroller - uisearchbar swift 4



searchDisplayController en desuso en iOS 8 (4)

¿Cómo corrige lo siguiente para que no aparezcan advertencias? ¿Qué me estoy perdiendo?

Cuando se corrige searchResultsController a searchController , searchController un error "objeto no encontrado"

if (tableView == self.searchDisplayController.searchResultsTableView) { cell.textLabel.text = [searchResults objectAtIndex:indexPath.row]; } else { cell.textLabel.text = [_content objectAtIndex:indexPath.row]; } return cell; } -(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString { [self filterContentForSearchText:searchString scope:[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]]; return YES; }


La clase UISearchController reemplaza a la clase UISearchDisplayController para administrar la visualización de las interfaces relacionadas con la búsqueda.

Fuente: https://developer.apple.com/library/ios/releasenotes/General/WhatsNewIniOS/Articles/iOS8.html

Entonces, como dijo rmaddy, si quieres deshacerte de las advertencias desaprobadas, deja de usar las clases desaprobadas. Utilice UISearchController en UISearchController lugar:

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UISearchController/index.html#//apple_ref/occ/cl/UISearchController


Agrega esto a tu archivo .h

<UISearchBarDelegate,UISearchResultsUpdating> NSArray *searchResultsArray; NSMutableArray *userMutableArray; @property (retain, nonatomic) UISearchController *searchController;

y esto a tu archivo .m

userMutableArray = [[NSMutableArray alloc] initWithObjects:@"Jack",@"Julie", nil]; searchResultsArray = [[NSArray alloc]init]; self.searchController = [[UISearchController alloc]initWithSearchResultsController:nil]; self.searchController.searchBar.scopeButtonTitles = [[NSArray alloc]initWithObjects:@"UserId", @"JobTitleName", nil]; self.searchController.searchBar.delegate = self; self.searchController.searchResultsUpdater = self; [self.searchController.searchBar sizeToFit]; self.searchController.dimsBackgroundDuringPresentation = NO; self.definesPresentationContext = YES; self.tableView.tableHeaderView = self.searchController.searchBar; -(void)updateSearchResultsForSearchController:(UISearchController *)searchController{ NSString *searchString = self.searchController.searchBar.text; NSPredicate *resultPredicate; NSInteger scope = self.searchController.searchBar.selectedScopeButtonIndex; if(scope == 0){ resultPredicate = [NSPredicate predicateWithFormat:@"userId contains[c] %@",searchString]; }else{ resultPredicate = [NSPredicate predicateWithFormat:@"jobTitleName contains[c] %@",searchString]; } searchResultsArray = [userMutableArray filteredArrayUsingPredicate:resultPredicate]; [self.tableView reloadData]; } - (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope{ [self updateSearchResultsForSearchController:self.searchController]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if(self.searchController.active){ return [searchResultsArray count]; }else{ return [userMutableArray count]; } } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellIdentifier; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; if(!cell){ cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; } if(self.searchController.active){ cell.textLabel.text = [searchResultsArray objectAtIndex:indexPath.row]; }else{ cell.textLabel.text = [userMutableArray objectAtIndex:indexPath.row]; } return cell; }


Estaba buscando migrar de UISearchDisplayController a UISearchController . Entonces, encontré esto en GitHub: https://github.com/dempseyatgithub/Sample-UISearchController
Descargue / clone y podrá ver cómo utilizar UISearchController con UITableView y UICollectionView .
Tiene todo lo que necesita para actualizar de UISearchDisplayController a UISearchController .
La documentación de UISearchController también es muy útil. Para comenzar, consulte Sample-UISearchController / MasterViewController_TableResults.m y Sample-UISearchController / MasterViewController_FilterResults.m
Si también necesita compatibilidad con iOS 7 (algo que personalmente recomiendo si realmente está a punto de implementar su aplicación en la App Store) haga esto:

if([UISearchController class]){ //Create an UISearchController and add it to your UITableViewController }else{ //Create an UISearchDisplayController and add it to your UITableViewController }

Nota: Tendrá que hacer todo programáticamente si desea admitir ambas versiones de iOS.


Llevamos mucho tiempo trabajando para que el nuevo UISearchController gire correctamente.

Esto es lo que parecía antes:

Después de mucho tiempo, esencialmente nos "dimos por vencidos" al hacer que la rotación funcionara correctamente. En su lugar, simplemente ocultamos el controlador de búsqueda antes de que ocurra la rotación. Luego, el usuario tiene que tocar el botón de búsqueda en la vista girada para que aparezca la barra de búsqueda nuevamente.

Aquí está el código relevante:

- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator { [self.searchController dismissViewControllerAnimated:YES completion:nil]; self.searchControllerWasActive = NO; self.searchButton.enabled = YES; }

Nota importante: nuestro código utiliza un UIViewController y no UITableViewController. La pantalla requiere botones adicionales, por lo que no podemos utilizar UITableViewController. El uso de UISearchController en un UITableViewController no presenta los problemas de rotación.

Vemos esto como una solución necesaria dado el estado actual de UISearchController. Sería mucho mejor tener una solución real a este problema.