2015-02-06 6 views
13

Próbuję uzyskać kontroler UISearch do pracy z kontrolerem UITableViewController. Tworzę wszystko programowo (tzn. Nie ma programu budującego interfejs). Przeważnie wszystko działa tak, jak powinno, z tym wyjątkiem, że podczas wyszukiwania nie mogę wybrać żadnej z komórek tabeli (-tableView:didSelectRowAtIndexPath: nigdy nie jest wywoływana).UISearchController w UITableViewController-Rows nie można wybrać, a widok tabeli jest wyszarzony

Jednak nie uważam, że jest to problem dotyczący delegatów lub innej powszechnej przyczyny, dla której ta metoda nie jest wywoływana; podczas wyszukiwania widok tabeli zawierający wyniki wyszukiwania jest wyszarzony (tzn. nieaktywny) i uważam, że dlatego nie mogę wybrać żadnych komórek.

Myślałem, że UISearchBar utknął jako pierwszy odpowiadający, ale próbowałem różnych sposobów rezygnacji z tego statusu i/lub próbując zmusić widok tabeli do stania się pierwszym reagującym, ale wszystko to miało katastrofalne skutki.

Oto stosowny kod z mojego UITableViewController podklasy:

@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 

Odpowiedz

19

Być może jest to spowodowane prezentuje wyniki wyszukiwania w tym samym widoku, które są szukasz? Spróbuj uniemożliwić przyciemnianie widoku ...

self.searchController.dimsBackgroundDuringPresentation = NO; 
+0

To był właśnie problem. I wstyd mi, ponieważ dokumentacja API wyraźnie wspomina o tym kodzie, jest wymagana podczas wyszukiwania/prezentowania w tym samym widoku. Dziękuję Ci bardzo! – FAVisceglia

+0

Dziękuję bardzo !!! – Crash

Powiązane problemy