2012-07-28 9 views
6

Sto implementando un UISearchDisplayController e desidero popolare il searchResultsTableView con il contenuto di tableView (non filtrato) direttamente sul caricamento - prima che venga inserito il testo.Come mostrare searchResultsTableView quando la barra di ricerca è attivata?

Questo funziona quando inizio a immettere valori nella barra di ricerca.

- (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]; 
} 

Qualcuno ha qualche indicatore nella giusta direzione?

Si prega di non rispondere con "non farlo" o "Apple non ha progettato il controllo in questo modo". o ...

+0

Hai trovato una soluzione? Ho esattamente lo stesso problema. – nonamelive

+0

Qualcuno ha ancora capito come farlo? – Chicken

risposta

0

il controllo di Apple funziona così L'ho risolto implementando un oggetto controller separato (chiamalo search display controller) che è una barra di ricerca delegata e applica un predicato all'origine dati. In questo modo la vista tabella viene filtrata 'in linea'.

0

È necessario ricaricare la visualizzazione della tabella con il risultato filtrato durante la ricerca sta facendo: In questo metodo basta chiamare vista tabella reloadData:

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

e modificare il metodo di numberOfSectionsInTableView come:

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

Spero che ti possa aiutare.

+0

Ciao Midhun, grazie per il tuo commento ma non funziona in questo modo. Quando attivo la ricerca, searchDisplayController riduce il valore di tableView (sebbene continuiamo a vedere il contenuto ma non possiamo controllarlo). Quando il testo viene inserito nella barra di ricerca, searchDisplayTableView viene caricato con il contenuto (filtrato o meno, che posso controllare), che voglio mostrare. – Ron

+0

@Ronny: quando si inizia a digitare la vista tabella si riduce, dopo aver digitato una singola lettera la vista tabella passa allo stato precedente (non disattivato). Penso che sia il tuo problema, ho ragione? –

+0

@Ronny: hai ancora il problema? –

0
- (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]; 
} 

È necessario ricordare di rimuovere _tempRecentSearchTableView in alcuni casi. Ad esempio:

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

si sa se la barra di ricerca è attiva.

- (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]; 

    } 
} 

Durante la configurazione della cella di fare la stessa domanda

- (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; 
} 

Impostare i parametri del filtro

-(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]; 
} 

spero di servire voi, il mio inglese è molto limitato

Problemi correlati