2015-02-06 10 views
13

Sto cercando di far funzionare UISearchController all'interno di UITableViewController. Sto creando tutto a livello di codice (cioè nessun costruttore di interfacce). Principalmente tutto funziona come dovrebbe, tranne che durante la ricerca, non riesco a selezionare nessuna delle celle della tabella (-tableView:didSelectRowAtIndexPath: non viene mai chiamato).UISearchController in un UITableViewController - Le righe non selezionabili e la vista tabella non sono selezionabili

Tuttavia, non credo che questo sia un problema relativo ai delegati o un'altra causa comune per il mancato richiamo di quel metodo; durante la ricerca, la vista tabella contenente i risultati della ricerca appare in grigio (cioè inattiva), e credo che sia il motivo per cui non riesco a selezionare alcuna cella.

Il mio pensiero era che l'UISearchBar è stato bloccato come prima responder, ma ho provato diversi modi di dimissioni che lo stato e/o cercando di forzare la visualizzazione della tabella di diventare il primo risponditore, ma tutto questo ha avuto risultati disastrosi.

Ecco il codice pertinente dal mio UITableViewController sottoclasse:

@interface SearchResultsTableViewController() 

@property (copy, nonatomic) NSArray *searchResults; 

@property (strong, nonatomic) UISearchController *searchController; 

@end 


@implementation SearchResultsTableViewController 

- (NSArray *)searchResults 
{ 
    // Lazy array instantiation 
    if (!_searchResults) 
    { 
     _searchResults = [[NSArray alloc] init]; 
    } 

    return _searchResults; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    [self setSearchController:[[UISearchController alloc] initWithSearchResultsController:nil]]; 
    [[self searchController] setSearchResultsUpdater:self]; 

    [[[self searchController] searchBar] setFrame:CGRectMake(0.0f, 0.0f, CGRectGetWidth([[self tableView] frame]), 44.0f)]; 
    [[[self searchController] searchBar] setSearchBarStyle:UISearchBarStyleMinimal]; 
    [[[self searchController] searchBar] setPlaceholder:@"Search Presentations"]; 
    [[[self searchController] searchBar] setDelegate:self]; 

    [[self tableView] setTableHeaderView:[[self searchController] searchBar]]; 
    [[self tableView] setSeparatorStyle:UITableViewCellSeparatorStyleNone]; 
    [[self tableView] setAllowsSelection:YES]; 
    [[self tableView] setAllowsSelectionDuringEditing:YES]; 
    [[self tableView] setDelegate:self]; 
    [[self tableView] setDataSource:self]; 
} 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{  
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Search Results Cell"]; 

    [cell setSelectionStyle:UITableViewCellSelectionStyleDefault]; 
    [cell setUserInteractionEnabled:YES]; 
    [[cell textLabel] setEnabled:YES]; 

    FXIPresentation *presentation = nil; 

    presentation = [[self searchResults] objectAtIndex:[indexPath row]]; 

    [[cell textLabel] setText:[[presentation title] substringFromIndex:3]]; 
    [[cell textLabel] setTextColor:[UIColor colorWithRed:(18.0/255.0) 
               green:(52.0/255.0) 
               blue:(88.0/255.0) 
               alpha:1.0f]]; 

    return cell; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Omitted/Never called 
} 

- (void)updateSearchResultsForSearchController:(UISearchController *)searchController 
{ 
    [self setSearchResults:nil]; 

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"title contains[c] %@", [[[self searchController] searchBar] text]]; 
    [self setSearchResults:[[self fullResults] filteredArrayUsingPredicate:predicate]]; 

    [[self tableView] reloadData]; 
} 

@end 

risposta

19

Forse è dovuto alla presentazione dei risultati della ricerca nella stessa vista che si sta cercando? Prova a impedire che la vista si oscuri ...

self.searchController.dimsBackgroundDuringPresentation = NO; 
+0

Questo era esattamente il problema. E vergogna per me, perché la documentazione API menziona in modo specifico questo codice quando si cerca/si presenta nella stessa vista. Grazie mille! – FAVisceglia

+0

Grazie mille !!! – Crash

Problemi correlati