2014-04-20 10 views
6

Sto utilizzando un UISearchDisplayController nella mia app. Quando l'utente seleziona un elemento nei risultati di ricerca restituiti, disattivo UISearchDisplayController. La disattivazione del controller cancella il testo digitato dall'utente. Voglio tenerlo lì. Provo ad assegnare il testo indietro a UISearchBar impostandolo nuovamente dopo che il controller è stato disattivato. Il testo appare nella barra di ricerca, ma questo renderà UISearchDisplayController nuovamente attivo anche se ho disabilitato il delegato! Questo problema si verifica solo su iOS 7. Prima di iOS7, il codice seguente funziona in modo affascinante.Come mantenere il testo nella barra di ricerca quando si rifiuta UISearchDisplayController

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 
NSUInteger row = [indexPath row]; 
NSString *term = [keywordSuggestion objectAtIndex:row]; 

[search resignFirstResponder]; 
[self handleSearchForTerm:term]; 
} 

-(void)handleSearchForTerm:(NSString *)searchTerm { 

[searchDisplayController setActive:NO animated:YES]; //searchbar text will be lost 

searchDisplayController.delegate = nil; 
search.text = term; 
searchDisplayController.delegate = self; 
} 

è che ci sono un modo che io possa impostare il testo della UISearchBar senza avere l'UISearchDisplayController che è associato con il diventare attivo?

risposta

1

Ecco un esempio di codice di lavoro:

#import "RBTableViewController.h" 

@interface RBTableViewController() <UISearchDisplayDelegate> 

@end 

@implementation RBTableViewController { 
    UISearchBar *_searchBar; 
    UISearchDisplayController *_searchController; 
    NSMutableArray *_searchResults; 
    NSMutableArray *_model; 
    NSString *_cachedSearchTerm; 
} 

- (NSMutableArray *)currentModel { 
    return _searchController.isActive ? _searchResults : _model; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    _searchResults = [[NSMutableArray alloc] init]; 
    _model = [[NSMutableArray alloc] init]; 

    for (int i = 0; i < 10; i++) { 
     [_model addObject:[NSString stringWithFormat:@"item %d", i]]; 
    } 

    _searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)]; 
    _searchController = [[UISearchDisplayController alloc] initWithSearchBar:_searchBar contentsController:self]; 
    _searchController.searchResultsDataSource = self; 
    _searchController.searchResultsDelegate = self; 
    _searchController.delegate = self; 
    [_searchController.searchResultsTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"]; 
    self.tableView.tableHeaderView = _searchBar; 
} 

- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller { 
    controller.searchBar.text = _cachedSearchTerm; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (tableView == _searchController.searchResultsTableView) { 
     _cachedSearchTerm = _searchBar.text; 
     [_searchController setActive:NO animated:YES]; 
     [self filterResults:_cachedSearchTerm]; 
    } 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return [[self currentModel] count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; 
    cell.textLabel.text = [self currentModel][indexPath.row]; 
    return cell; 
} 

- (void)filterResults:(NSString *)searchTerm { 
    [_searchResults removeAllObjects]; 
    [_searchResults addObjectsFromArray:[_model filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF contains[s] %@", searchTerm]]]; 
    [_searchController.searchResultsTableView reloadData]; 
} 

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString { 
    [self filterResults:searchString]; 
    return YES; 
} 

- (void)searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller { 
    if (_cachedSearchTerm) { 
     controller.searchBar.text = _cachedSearchTerm; 
    } 
} 

@end 
Problemi correlati