2014-09-19 60 views
2

我有我添加以編程方式的UISearchBar這樣的(這是我的viewDidLoad)一個表視圖控制器:的UISearchBar時以編程方式添加不`噸調用didSelectRowAtIndexPath方法

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

我dont`t設置爲tableHeaderView因爲我對標題有另一種看法(藍色)。

My Tableview

問題是,當我搜索並點擊單元格,方法didSelectRowAtIndexPath方法沒有被調用。而且,正如它在我的圖片上顯示的那樣,我需要知道選中的單元格是否添加了複選標記。

回答

1

您還需要指定'self'作爲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。

相關問題