搜索框可以用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
时间: 2024-10-11 06:06:34