TableView search results not selected

In my application, I have a TableView full of cells. Everything works really well. When I click on a cell, it immediately calls tableView:DidSelectRowAtIndexPath:

and executes the session, taking me to the next screen. I also have a search bar using a UISearchDisplayController to allow users to search for items in a tableView. When I enter text into the search bar, the cells match the lookup display in the table.

Now, my problem is that I click on one of the cells displayed in this search results table ... On one first click, the table view does not respond in any way. If the short press is briefly held down, the cell turns gray as if it had been selected, but tableView:DidSelectRowAtIndexPath:

still not called, and when released, the tap returns to normal. But if I do a long print for a few seconds, then it tableView:DidSelectRowAtIndexPath:

finally gets called and I am taken to the correct screen.

Has anyone encountered this problem before? As far as I know I have implemented my UISearchDisplayController in the same way as always and never had this problem.

Thank you and let me know if I can provide additional information that might be helpful

EDIT

I'm not sure what the exact problem is, so I'm not sure which methods to show, but here's some code ... I bring up the search bar by clicking an icon in the UINavigationBar and then remove it from the supervisor after editing is complete.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    NSArray *contentArray;
    if (tableView == self.tableView) {
        contentArray = self.postArray;
    } else if (tableView == self.searchDisplayController.searchResultsTableView) {
        contentArray = self.searchResults;
    }

    // This is pretty hackish, but it wasn't working before for some reason. So I send the PFObject I want as the sender
    [self performSegueWithIdentifier:@"ShowPostDetails" sender:contentArray[indexPath.row]];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"PostCell";

    PostTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell) {
        cell = [[PostTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    [self updateSubviewsForCell:cell inTableView:tableView atIndexPath:indexPath];
/*
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        [cell addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cellTapped:)]];
    } */

    return cell;
}

#pragma mark - Search Bar Delegate

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
    NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"text contains[c] %@", searchText];
    self.searchResults = [self.postArray filteredArrayUsingPredicate:resultPredicate];
}

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    [self filterContentForSearchText:searchString
                               scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
                                      objectAtIndex:[self.searchDisplayController.searchBar
                                                     selectedScopeButtonIndex]]];

    return YES;
}

- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller
{
    [self.view addSubview:self.searchDisplayController.searchBar];
    self.searchDisplayController.searchBar.center = CGPointMake(self.view.window.center.x, 42);
}

- (void)searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller
{
    [self.searchDisplayController.searchBar removeFromSuperview];
}

      

+3


source to share


1 answer


I found my problem ...

The particular view controller that was experiencing this issue is subclassed from the view controller containing these delegate methods and contains a UITextField for inputting information. I watch the keyboardDidAppear notification and when it appears I add a UITapGestureRecognizer to the view to close the keyboard, canceling the first UITextField responder while listening to the view. When I added this, I hadn't implemented the search function yet, so I knew the only reason for the keyboard to appear for the UITextField. The problem is that this additional TapGestureRecognizer was added when the keyboard popped up to the search bar to prevent the TapGestureRecognizer from being included in the UITableView cell. I was looking for the wrong place for the problem. To solve the problem, I just did a check,that UITextField is indeed the first responder before adding the gesture recognizer. Everything now works as expected.



So, for everyone else experiencing a similar issue, I would say go back and make sure you don't have any other UIGestureRecognizers that might conflict with your TableView's gesture recognizers.

Thanks to everyone who commented. Helped lead me to where the problem was.

+1


source







All Articles