简体   繁体   中英

Search Results TableView not getting selected

In my application I have a TableView full of cells. Everything works just fine -- When I tap on a cell, it calls tableView:DidSelectRowAtIndexPath: right away and executes a segue, bringing me to the next screen. I also have a search bar, using UISearchDisplayController, allowing users to search through the items in the tableView. When I type some text into the search bar, the cells that match the search display in the table.

Now, my problem is when I tap on one of the cells displayed in this search results table... On one initial tap, the table view does not respond in any way. If the tap is held just briefly, the cell turns gray, as if it were selected, however tableView:DidSelectRowAtIndexPath: is still not called, and the cell turns back to normal after releasing the tap. But if I do a long press for a few seconds, then tableView:DidSelectRowAtIndexPath: is finally called, and I am brought to the correct screen.

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

Thank You, and let me know if I can give any additional information that may be helpful

EDIT

I am not certain wherein the problem lies exactly, so I'm not sure which methods to show, but here is some code... I am bringing up the search bar upon clicking an icon in the UINavigationBar, then removing it from the superview once the editing has finished.

- (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];
}

I found my problem...

The specific view controller that was experiencing this problem is subclassed from the view controller containing these delegate methods, and contains a UITextField for entering information. I watch for a keyboardDidAppear notification, and when it appears I add a UITapGestureRecognizer to the view to close the keyboard by resigning first responder of the UITextField when the view is tapped. When I added this I had not yet implemented the search feature, so I knew the only reason the keyboard would pop up is for the UITextField. The problem is that this extra TapGestureRecognizer added when the keyboard popped up for the search bar prevented the TapGestureRecognizer built into the UITableView cell from firing. I was looking in the wrong spot for the problem. To fix the problem I simply made a check that the UITextField is indeed the first responder before adding the gesture recognizer. Now all works as it is supposed to.

So for anyone else experiencing a similar problem, I'd say go back and make sure you don't have any other UIGestureRecognizers that might be conflicting with the gesture recognizers of your tableView.

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

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