iOS开发实战之搜索控制器UISearchController使用

当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);

你可能感兴趣的:(iOS开发)