當tableView中的數據過多的時候,在tableView上加一個搜索框就變的很必要了,本文就討論搜索控制器的使用,以及謂詞的簡單實現。
.m文件中代碼如下
添加搜索控制器的各種協議
<UISearchBarDelegate,UISearchResultsUpdating,UIAlertViewDelegate>
創建兩個實例和數據源
{ NSArray * _dataSource; } @property(nonatomic,strong)UISearchController *searchController;//搜索控制器 @property (strong, nonatomic)NSMutableArray *searchList;//滿足搜索條件的數組
初始化搜索控制器的各種屬性
-(void)initMysearchBarcontroller { _searchController=[[UISearchController alloc]initWithSearchResultsController:nil]; //設置背景不透明 _searchController.searchBar.translucent=NO; _searchController.searchBar.barTintColor=[UIColor brownColor]; //設置searchbar的邊框顏色和背景顏色一致 _searchController.searchBar.layer.borderWidth=1; _searchController.searchBar.layer.borderColor=[[UIColor brownColor] CGColor]; _searchController.searchBar.placeholder=@"搜索聯系人"; _searchController.searchResultsUpdater = self; _searchController.dimsBackgroundDuringPresentation = NO; _searchController.hidesNavigationBarDuringPresentation = NO; _searchController.searchBar.frame = CGRectMake(self.searchController.searchBar.frame.origin.x, self.searchController.searchBar.frame.origin.y, self.searchController.searchBar.frame.size.width, 44); _searchController.searchBar.delegate=self; self.tableView.tableHeaderView = self.searchController.searchBar; //清空tableview多余的空格線 [self.tableView setTableFooterView:[[UIView alloc]initWithFrame:CGRectZero]]; }
在輸入搜索框中輸入文本執行的代理函數
//每輸入一個字符都會執行一次 -(void)updateSearchResultsForSearchController:(UISearchController *)searchController { NSString *searchString = [self.searchController.searchBar text]; //謂詞搜索 NSPredicate *preicate = [NSPredicate predicateWithFormat:@"SELF.name CONTAINS[c] %@", searchString]; if (self.searchList!= nil) { [self.searchList removeAllObjects]; } //過濾數據 self.searchList= [NSMutableArray arrayWithArray:[_dataSource filteredArrayUsingPredicate:preicate]]; //刷新表格 [self.tableView reloadData]; }
在這個時候 就可以獲得符合你的搜索條件的數值了。
如果你想改變tableView的顯示內容,記得reloadData。
NSPredicate *preicate = [NSPredicate predicateWithFormat:@"SELF.name CONTAINS[c] %@", searchString];
這里的SELF.name就是你數據源里的model的屬性。SELF就是model的類,這里是按照名字搜索。
獲取到篩選數組后,就要改你的tableView上顯示的東西了。
例如:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { //控制器使用的時候,就是點擊了搜索框的時候 if (self.searchController.active) { return self.searchList.count; } //控制器未使用的時候 return _dataSource.count; }
還有其他tableView的代理函數也要改。
全部寫完后,會發現點了搜索框之后右邊有個取消的英文。怎么變中文呢,如下
-(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar { [searchBar setShowsCancelButton:YES animated:YES]; for (id obj in [searchBar subviews]) { if ([obj isKindOfClass:[UIView class]]) { for (id obj2 in [obj subviews]) { if ([obj2 isKindOfClass:[UIButton class]]) { UIButton *btn = (UIButton *)obj2; [btn setTitle:@"取消" forState:UIControlStateNormal]; } } } } return YES; }
怎么截取那個取消按鈕的代理呢,給你官方的。
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar; // return NO to not become first responder
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar; // called when text starts editing
- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar; // return NO to not resign first responder
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar; // called when text ends editing
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText; // called when text changes (including clear)
- (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text NS_AVAILABLE_IOS(3_0); // called before text changes
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar; // called when keyboard search button pressed
- (void)searchBarBookmarkButtonClicked:(UISearchBar *)searchBar __TVOS_PROHIBITED; // called when bookmark button pressed
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar __TVOS_PROHIBITED; // called when cancel button pressed
- (void)searchBarResultsListButtonClicked:(UISearchBar *)searchBar NS_AVAILABLE_IOS(3_2) __TVOS_PROHIBITED; // called when search results button pressed
- (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope NS_AVAILABLE_IOS(3_0);
原文鏈接:https://blog.csdn.net/clmd_ld/article/details/50782510/