美文网首页
UISearchController使用及常见问题处理

UISearchController使用及常见问题处理

作者: jazzfly | 来源:发表于2019-10-08 15:00 被阅读0次

    项目实战开发过程会经常使用到搜索功能,使用系统提供的控件无疑是最方便最简单的,有时也是遇到问题也是较难解决的,接下来我们来分析下UISearchController使用及常见问题的解决:
    1.UISearchController基本使用
    初始化UISeachBar

    #pragma mark - 创建顶部搜索框
    - (UISearchBar *)createSearchBar
    {
        WEAKSELF
        SearchGroupMemberListController *searchVC = [[SearchGroupMemberListController alloc]init];
        searchVC.identifierString = @"TT_SearchReplyContact";
        searchVC.definesPresentationContext = YES;
        if (self.navigationController.viewControllers.count > 1) {
            searchVC.isHomePage = NO;
        }else{
            searchVC.isHomePage = YES;
        }
        
        searchVC.searchResultsTableViewCellSelectedBlock = ^(NSIndexPath *indexPath,NSMutableArray *dataArray){
            GroupMemberInfo *groupMemberInfo = (GroupMemberInfo *)dataArray[indexPath.row];
            [weakSelf.delegate replyNickNameFromCell:groupMemberInfo];
            [weakSelf dismissViewControllerAnimated:NO completion:^{
                [weakSelf cancel];
            }];
        };
        
        _mySearchController = [[UISearchController alloc] initWithSearchResultsController:searchVC];
        _mySearchController.delegate = self;
        _mySearchController.searchResultsUpdater = self;//搜索结果代理
        _mySearchController.dimsBackgroundDuringPresentation = YES;
        if (@available(iOS 9.1, *)) {
            _mySearchController.obscuresBackgroundDuringPresentation = NO;
        }
        _mySearchController.hidesNavigationBarDuringPresentation = YES;
        _mySearchController.searchBar.placeholder = @"搜索";
        [_mySearchController.searchBar setValue:@"取消"forKey:@"_cancelButtonText"];
        _mySearchController.searchBar.tintColor = kTextColor_170;
        _mySearchController.searchBar.searchBarStyle = UISearchBarStyleMinimal;
        _mySearchController.searchBar.backgroundColor = kBackgroundColor;
        UITextField *searchField = [_mySearchController.searchBar valueForKey:@"_searchField"];
        searchField.backgroundColor = kSearchBackColor;
        searchField.textColor = kTextColort_238;
        searchField.tintColor = kTextColor_170;
        [_mySearchController.searchBar sizeToFit];
        return _mySearchController.searchBar;
    }
    

    2.将searchBar添加到tableView的tableHeaderView上即可

    3.UISearchController代理UISearchControllerDelegate

    点击searchBar,即将进入搜索模式
    - (void)willPresentSearchController:(UISearchController *)searchController
    {
          self.replyTableView.hidden = YES;//隐藏底部view
        self.navigationController.navigationBar.translucent = YES;//设置导航栏半透明
        searchController.searchBar.placeholder = @"选择回复的人";//搜索占位符
    }
    //完全进入搜索模式
    - (void)didPresentSearchController:(UISearchController *)searchController
    {
    
    }
    //点击取消按钮,即将关闭搜索模式
    - (void)willDismissSearchController:(UISearchController *)searchController
    {
          self.replyTableView.hidden = NO;//取消隐藏
        self.navigationController.navigationBar.translucent = NO;//取消导航栏半透明
        searchController.searchBar.placeholder = @"搜索";
    }
    //完全关闭搜索模式
    - (void)didDismissSearchController:(UISearchController *)searchController
    {
    
    }
    

    4.UISearchController搜索结果代理UISearchResultsUpdating

    #pragma mark - UISearchResultsUpdating
    - (void)updateSearchResultsForSearchController:(UISearchController *)searchController{
         NSString *searchString = searchController.searchBar.text;
        if (!ISNULLSTR(searchString)) {
            if (!ISNULLARRAY(self.searchResultTableDataSource)) {
                [self.searchResultTableDataSource removeAllObjects];
            }
            NSArray *searchResult = [self sortSearchResult:searchString];
            if (!ISNULLARRAY(searchResult)) {
                self.searchResultTableDataSource = [searchResult mutableCopy];
            }
            SearchGroupMemberListController *vc = (SearchGroupMemberListController *)searchController.searchResultsController;
            vc.dataArray = self.searchResultTableDataSource;
            [vc.searchTableView reloadData];
        }
    }
    //模糊筛选好友
    - (NSMutableArray *)sortSearchResult:(NSString *)searchString
    {
        if (ISNULLSTR(searchString)) {
            return nil;
        }
        NSPredicate *nickNamePredicate  = [NSPredicate predicateWithFormat:@"user_name CONTAINS[cd] %@",searchString];
        NSPredicate *TTNumPredicate = [NSPredicate predicateWithFormat:@"ttString CONTAINS[cd] %@",searchString];
        NSMutableArray *array = [NSMutableArray arrayWithArray:self.memberListArray];
        
        NSPredicate *predicate = [NSCompoundPredicate orPredicateWithSubpredicates:@[nickNamePredicate, TTNumPredicate]];
        [array filterUsingPredicate:predicate];
        NSSortDescriptor *NameSortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"user_name"ascending:YES];
        NSArray *Sortarray = [array sortedArrayUsingDescriptors:@[NameSortDescriptor]];
        return  [NSMutableArray arrayWithArray:Sortarray];
    

    5.常见问题总结:
    1.取消按钮的中文设置,通过KVC进行修改
    [_mySearchController.searchBar setValue:@"取消"forKey:@"_cancelButtonText"];
    2.点击搜索结果控制器跳转问题
    searchVC.definesPresentationContext = YES;
    3.取消searchBar的第一响应后,点击取消按钮无反应
    原理是cancleBtn.enable=NO,需要手动设置为YES,具体获取cancleBtn的方法,在网上找找有很多,就不多介绍。

    相关文章

      网友评论

          本文标题:UISearchController使用及常见问题处理

          本文链接:https://www.haomeiwen.com/subject/kxfsyctx.html