標籤:
搜尋方塊可以用UISearchBar,並且可以設定代理UISearchBarDelegate。
1 -(void)addSearchBar{ 2 CGRect searchBarRect = CGRectMake(0, 0, self.view.frame.size.width, 44); 3 UISearchBar *searchBar = [[UISearchBar alloc]initWithFrame:searchBarRect]; 4 searchBar.placeholder = @"plrase enter key word"; 5 self.tableView.tableHeaderView = searchBar; 6 //設定搜尋方塊旁邊的取消按鈕 7 searchBar.showsCancelButton = YES; 8 //設定搜尋範圍 9 searchBar.showsScopeBar = YES;10 searchBar.scopeButtonTitles = [NSArray arrayWithObjects:@"姓名",@"性別",@"學號", nil];11 //代理12 searchBar.delegate = self;13 }
效果
對結果搜尋的處理也不是很麻煩,原理就是字串中是否包含搜尋的關鍵字,然後重新整理tableView
//contact.firstName為所有資料,keyWord為搜尋的關鍵字if ([contact.firstName.uppercaseString containsString:keyWord.uppercaseString]) { [_searchContacts addObject:contact];}
實現代理可以用
#pragma mark 搜尋方塊代理#pragma mark 取消搜尋-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{ //放棄第一響應者對象,關閉軟鍵盤 [self.searchBar resignFirstResponder];}#pragma mark 輸入搜尋索引鍵-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{ }#pragma mark 點擊虛擬鍵盤上的搜尋時-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{ [self.searchBar resignFirstResponder];}
效果如下:
也可以用更簡單的UISearchDisplayController,它內部有一個UITableView類型的對象searchResultsTableView,就可以不用上邊的3個代理了。只需實現1個。UISearchDisplayController在使用UINavavigationController時還可以自動全屏,效果為
#pragma mark - UISearchDisplayController代理方法-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString{ [self searchDataWithKeyWord:searchString]; return YES;}
添加UISearchDisplayController時,有兩種寫法。需要注意,否則會報
Property ‘searchDisplayController‘ attempting to use instance variable ‘_searchDisplayController‘ declared in super class ‘UIViewController‘
第一種,用property形式,需要@synthesize修飾才可以
@interface ContactsTableViewController ()<UISearchBarDelegate,UISearchDisplayDelegate>@property(nonatomic,strong) UISearchBar *searchBar;@property(nonatomic,strong) UISearchDisplayController *searchDisplayController;@end@implementation ContactsTableViewController//需要用synthesize修飾@synthesize searchDisplayController;//此時的添加搜尋欄可以用-(void)addSearchBar{ _searchBar=[[UISearchBar alloc]init]; [_searchBar sizeToFit];//大小自適應容器 _searchBar.placeholder = @"plrase enter key word"; _searchBar.autocapitalizationType = UITextAutocapitalizationTypeNone; _searchBar.showsCancelButton = YES;//顯示取消按鈕 _searchBar.delegate=self; self.tableView.tableHeaderView=_searchBar; self.searchDisplayController = [[UISearchDisplayController alloc]initWithSearchBar:self.searchBar contentsController:self]; self.searchDisplayController.delegate = self; self.searchDisplayController.searchResultsDataSource = self; self.searchDisplayController.searchResultsDelegate = self; [self.searchDisplayController setActive:NO animated:YES];}@end
第二種,用帶底線的形式,無需@synthesize修飾
@interface ContactsTableViewController ()<UISearchBarDelegate,UISearchDisplayDelegate>{ UISearchDisplayController *_searchDisplayController;}@property(nonatomic,strong) UISearchBar *searchBar;@end@implementation ContactsTableViewController//此時添加搜尋欄為-(void)addSearchBar{ self.searchBar = [[UISearchBar alloc]init]; [self.searchBar sizeToFit];//大小自適應容器 self.searchBar.placeholder = @"plrase enter key word"; self.searchBar.autocapitalizationType = UITextAutocapitalizationTypeNone; self.searchBar.showsCancelButton = YES;//顯示取消按鈕 self.searchBar.delegate=self; self.tableView.tableHeaderView = self.searchBar; _searchDisplayController = [[UISearchDisplayController alloc]initWithSearchBar:self.searchBar contentsController:self]; _searchDisplayController.delegate = self; _searchDisplayController.searchResultsDataSource = self; _searchDisplayController.searchResultsDelegate = self; [_searchDisplayController setActive:NO animated:YES];}@end
IOS中搜尋方塊UISearchBar及搜尋方法的使用