2011-09-14 237 views
0

我想修改tableSearch示例代碼,以便沒有用於tableview的筆尖。由於這已經是一個tableviewcontroller子類,我可以這樣調用的實現代碼如下:UISearchview響應操作 - TableSearch示例iphone

MainViewController *mainViewController = [[MainViewController alloc] initWithStyle:UITableViewStylePlain];//NibName:@"MainView" bundle:nil]; 

沒有筆尖,我想手動添加搜索欄。我試過如下:

UISearchBar* searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0,10, 320, 41)]; 
[searchBar setFrame:CGRectMake(0,10, 320, 41)]; 
searchBar.delegate = self; 
searchBar.tintColor = [UIColor redColor]; 
[searchBar sizeToFit]; 
self.tableView.tableHeaderView = searchBar; 

搜索欄確實顯示正常。現在,我希望此搜索欄的搜索操作指向我已在代碼中使用的方法,即searchDisplayController委託方法。 無論如何,我可以將self.searchDisplayController的搜索欄屬性指向我的自定義搜索欄,以便他們執行相同的操作?在此先感謝...

回答

1

您必須使用UISearchDisplayControllerinitWithSearchBar:contentsViewController方法。這樣,您的自定義搜索欄將與控制器相關聯。你也必須在這個方法中設置contentsViewController

+0

喜做了,但代碼:( – user542584

+0

你必須要更具體的地方失敗沒有影響...最好將它放入您的原來的問題。 – Mundi

0

嗯..這是我的一個VC的代碼。同時設定UISearchBarDelegate,UISearchDisplayDelegate

_listContainer = [[UITableView alloc] init]; 
_listContainer.delegate = self; 
_listContainer.dataSource = self; 
_listContainer.separatorStyle = UITableViewCellSelectionStyleNone; 
_data = [[NSArray alloc] init]; 

然後

_searchBar = [[NL_CustomSearchBar alloc] initWithFrame:CGRectMake(4.0f, 0, 150, 46)]; 
_searchBar.delegate = self; 
_listContainer.tableHeaderView = _searchBar;