简体   繁体   中英

UISearchController in a UITableViewController—Rows not selectable and table view is greyed out

I am trying to get a UISearchController to work within a UITableViewController. I am creating everything programmatically (ie no interface builder). Mostly everything is working as it should, except that while searching, I am unable to select any of the table cells ( -tableView:didSelectRowAtIndexPath: is never called).

However, I do not believe this is an issue pertaining to delegates or another common cause for that method not being called; while searching, the table view containing the search results appears greyed out (ie inactive), and I believe that is why I am unable to select any cells.

My thought was that the UISearchBar was stuck as a first responder, but I tried various ways of resigning that status and/or trying to force the table view to become the first responder, but all of that had disastrous results.

Here is the pertinent code from my UITableViewController subclass:

@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

Perhaps it is due to presenting the search results in the same view that you are searching? Try preventing the view from dimming...

self.searchController.dimsBackgroundDuringPresentation = NO;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM