Search code examples
iosuitableviewuisearchdisplaycontroller

Selections not occurring when UISearchDisplayController is being used


I am using UISeachDisplayController with a UITableView. It is set up programatically in the viewDidLoad method for the UITableViewController:

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self loadProjectsAndTitles];
    searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 600, 44)];
    searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar
                                                                contentsController:self];
    searchDisplayController.delegate = self;
    searchDisplayController.searchResultsDataSource = self;
    searchDisplayController.searchResultsTableView.allowsSelection = TRUE;
    self.tableView.tableHeaderView = searchBar;
}

Everything is working absolutely fine except that when I click on a row when the UISearchBar is being used, the - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath method is not being called although it is being called perfectly well when the UISearch is not being used. My question is is this normal behaviour or should the selection method work? If it should work are there any suggestions why it might not be working?


Solution

  • I think you forgot to set

    searchDisplayController.searchResultsDelegate = self;
    

    searchResultsDelegate is the delegate for the table view in which the search results are displayed, and didSelectRowAtIndexPath is one of the table view delegate methods.