以编程方式添加UISearchbar时不要调用didSelectRowAtIndexPath

时间:2014-09-19 13:04:06

标签: ios objective-c uitableview uisearchbar

我有一个表视图控制器,我以编程方式添加UISearchBar(这是在我的ViewDidLoad上):

searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 64)];
searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
searchDisplayController.delegate = self;
searchDisplayController.searchResultsDataSource = self;
searchBar.delegate = self;
[self.tableView.tableHeaderView addSubview:searchBar];
[self.tableView.tableHeaderView sizeToFit];

我没有设置为tableHeaderView,因为我在Header上有另一个视图(蓝色)。

My Tableview

问题是当我搜索并单击该单元格时,方法didSelectRowAtIndexPath未被调用。并且,正如它在我的图像上所示,我需要知道选择了哪个单元格确实添加了复选标记。

1 个答案:

答案 0 :(得分:1)

你需要分配自己的'作为searchResultsDelegate也是如此。像这样。

    searchDisplayController.searchResultsDelegate = self;

然后在didSelectRowForIndexPath中,您需要区分搜索表和常规表。像这样:

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if ( tableView == _searchController.searchResultsTableView) {
        // user selected a row in your search results table
    } else {
        // user selected a row in your main table
    }
}

您还需要在其他委托方法中进行此区分(对searchResultsTableView进行测试),如果有的话,例如tableView didDeselectRowAtIndexPath。