web-dev-qa-db-fra.com

searchDisplayController déconseillé dans iOS 8

Comment corrigez-vous ce qui suit afin qu'aucun avertissement n'apparaisse? Qu'est-ce que je rate?

Lors de la correction de searchResultsController en searchController cela me donne une erreur "objet non trouvé"

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;
}
25
George Asda

La classe UISearchController remplace la classe UISearchDisplayController pour gérer l'affichage des interfaces liées à la recherche.

Source: https://developer.Apple.com/library/ios/releasenotes/General/WhatsNewIniOS/Articles/iOS8.html

Donc, comme l'a dit rmaddy, si vous voulez vous débarrasser des avertissements obsolètes, arrêtez d'utiliser les classes obsolètes. Utilisez à la place UISearchController:

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

12
michaelsnowden

Ajoutez ceci à votre fichier .h

<UISearchBarDelegate,UISearchResultsUpdating>

NSArray *searchResultsArray;
NSMutableArray *userMutableArray;

@property (retain, nonatomic) UISearchController *searchController;

et ceci dans votre fichier .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;
}
9
RStack

Je cherchais à migrer de UISearchDisplayController vers UISearchController. Donc, j'ai trouvé cela sur GitHub: https://github.com/dempseyatgithub/Sample-UISearchController
Téléchargez/clonez-le et vous pourrez voir comment utiliser UISearchController avec UITableView et UICollectionView.
Il a tout ce dont vous avez besoin pour passer de UISearchDisplayController à UISearchController.
La documentation UISearchController est également très utile. Pour commencer, regardez l'exemple - UISearchController/MasterViewController_TableResults.m et Sample-UISearchController/MasterViewController_FilterResults.m
Si vous devez également prendre en charge iOS 7 (quelque chose que je recommande personnellement si vous êtes vraiment sur le point de déployer votre application sur l'App Store), procédez comme suit:

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

Remarque: vous devrez tout faire par programme si vous souhaitez prendre en charge les deux versions d'iOS.

4
Gabriel Tomitsuka

Nous travaillons depuis longtemps pour faire tourner correctement le nouveau UISearchController.

Voici à quoi cela ressemblait auparavant:

Après beaucoup de temps, nous avons essentiellement "abandonné" le bon fonctionnement de la rotation. Au lieu de cela, nous masquons simplement le contrôleur de recherche avant la rotation. L'utilisateur doit ensuite appuyer sur le bouton de recherche sur la vue pivotée pour afficher à nouveau la barre de recherche.

Voici le code pertinent:

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

Remarque importante: Notre code utilise un UIViewController et non UITableViewController. L'écran nécessite des boutons supplémentaires, c'est pourquoi nous ne pouvons pas utiliser UITableViewController. L'utilisation de UISearchController sur un UITableViewController ne présente pas les problèmes de rotation.

Nous considérons cela comme une solution nécessaire compte tenu de l'état actuel de UISearchController. Il vaudrait beaucoup mieux avoir une vraie solution à ce problème.

0
Stephen Mlawsky