無搜尋結果顯示
代理:<UISearchBarDelegate>
@protocol UISearchBarDelegate <NSObject> @optional - (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar; // return NO to not become first responder - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar; // called when keyboard search button pressed - (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope NS_AVAILABLE_IOS(3_0); @end |
帶有搜尋結果顯示
代理:<UITableViewDataSource, UITableViewDelegate,UISearchBarDelegate,UISearchDisplayDelegate>
@protocol UISearchDisplayDelegate <NSObject> @optional // when we start/end showing the search UI // called when the table is created destroyed, shown or hidden. configure as necessary. // called when table is shown/hidden // return YES to reload table. called when search string/option changes. convenience methods on top UISearchBar delegate methods @end |
#以下範例為使用UISearchDisplayDelegate代理的方法
搜尋元件初始化:
無scope的設定方式
searchData = [[NSMutableArray alloc] init]; |
引用代理方法:
//-----------------------------------搜尋---------------------------------------- - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString [searchData removeAllObjects]; |
有scope的設定方式
searchData = [[NSMutableArray alloc] init]; searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)]; searchBar.scopeButtonTitles=[NSArray arrayWithObjects:@"全部",@"加點",@"扣點", nil]; searchBar.tintColor=jobList.backgroundColor; //searchBar.tintColor=[UIColor greenColor]; // Do any additional setup after loading the view. /*the search bar widht must be > 1, the height must be at least 44 (the real size of the search bar)*/ searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self]; /*contents controller is the UITableViewController, this let you to reuse the same TableViewController Delegate method used for the main table.*/ searchDisplayController.delegate = self; searchDisplayController.searchResultsDataSource = self; searchDisplayController.searchResultsDelegate=self; //set the delegate = self. Previously declared in ViewController.h for (id subview in searchDisplayController.searchBar.subviews ) { if([subview isMemberOfClass:[UISegmentedControl class]]) { UISegmentedControl *scopeBar=(UISegmentedControl *) subview; [scopeBar setSegmentedControlStyle:UISegmentedControlStyleBordered]; [scopeBar setTintColor: [UIColor greenColor]];//you can also set RGB color here //scopeBar.tintColor = [UIColor blackColor]; } } self.jobList.tableHeaderView = searchBar; //this line add the searchBar |
引用代理方法:
//-----------------------------------搜尋---------------------------------------- -(void)searchDisplayController:(UISearchDisplayController *)controller didLoadSearchResultsTableView:(UITableView *)tableView // - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption |