簡體   English   中英

搜索結果TableView未被選中

[英]Search Results TableView not getting selected

在我的應用程序中,我有一個充滿單元格的TableView。 一切正常 - 當我點擊一個單元格時,它會立即調用tableView:DidSelectRowAtIndexPath:並執行一個segue,將我帶到下一個屏幕。 我還有一個搜索欄,使用UISearchDisplayController,允許用戶搜索tableView中的項目。 當我在搜索欄中輸入一些文本時,匹配搜索的單元格會顯示在表格中。

現在,我的問題是當我點擊此搜索結果表中顯示的其中一個單元格時......在一次初始點擊時,表格視圖不會以任何方式響應。 如果短暫地保持點擊,則單元格變為灰色,就像它被選中一樣,但是仍未調用tableView:DidSelectRowAtIndexPath:並且在釋放點擊后單元格恢復正常。 但是如果我長按幾秒鍾,那么最終會調用tableView:DidSelectRowAtIndexPath:並將我帶到正確的屏幕。

以前有人遇到過這個問題嗎? 據我所知,我已經實現了我以前的UISearchDisplayController,並且從未遇到過這個問題。

謝謝,如果我能提供任何可能有用的其他信息,請告訴我

編輯

我不確定問題究竟在哪里,所以我不確定要顯示哪些方法,但這里有一些代碼...我在點擊UINavigationBar中的圖標時調出搜索欄,然后從superview中刪除它編輯完成后。

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

我發現了我的問題......

遇到此問題的特定視圖控制器是從包含這些委托方法的視圖控制器創建的子類,並包含用於輸入信息的UITextField。 我看一下keyboardDidAppear通知,當它出現時,我在視圖中添加一個UITapGestureRecognizer,通過在點擊視圖時重新設置UITextField的第一個響應者來關閉鍵盤。 當我添加它時,我還沒有實現搜索功能,所以我知道鍵盤彈出的唯一原因是UITextField。 問題是,當為搜索欄彈出鍵盤時,這個額外的TapGestureRecognizer會阻止內置在UITableView單元格中的TapGestureRecognizer觸發。 我正在尋找問題的錯誤點。 為了解決這個問題,我只是在添加手勢識別器之前檢查了UITextField確實是第一個響應者。 現在所有的工作都應該如此。

因此,對於遇到類似問題的其他人,我會說回去確保您沒有任何其他可能與您的tableView的手勢識別器沖突的UIGestureRecognizer。

感謝所有評論的人。 幫助引導我解決問題所在。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM