美文网首页很常
OC UISearchController

OC UISearchController

作者: 喵喵粉 | 来源:发表于2021-04-02 11:58 被阅读0次

    UISearchController,可用将属性searchBar作为UITableViewtableHeaderView,设置代理,就能监听搜索文本变动。

    @interface SearchViewController () <UISearchControllerDelegate, UISearchResultsUpdating, UITableViewDelegate, UITableViewDataSource>
    @property (nonatomic, strong) UISearchController *searchVC;
    @property (nonatomic, strong) UITableView *tvList;
    
    @property (nonatomic, strong) NSArray<NSString *> *items;
    @property (nonatomic, strong) NSMutableArray<NSString *> *searchItems;
    @end
    
    @implementation SearchViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        self.view.backgroundColor = [UIColor whiteColor];
        
        [self setupUI];
        [self setupData];
    }
    
    - (void)setupData {
        NSMutableArray *tmps = [NSMutableArray array];
        for (int i = 0; i < 50; i++) {
            [tmps addObject:[NSString stringWithFormat:@"line %d", i]];
        }
        self.items = tmps;
        self.searchItems = [NSMutableArray array];
        
        [_tvList reloadData];
    }
    
    - (void)setupUI {
        self.tvList = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
        _tvList.delegate = self;
        _tvList.dataSource = self;
        [_tvList registerClass:UITableViewCell.class forCellReuseIdentifier:@"cell"];
        [self.view addSubview:_tvList];
        
        self.searchVC = [[UISearchController alloc] initWithSearchResultsController:nil];
        _searchVC.delegate = self;
        _searchVC.searchResultsUpdater = self;
        
        //If you use the same view controller to display the searchable content and search results,
        //it is recommended that you set this property to NO. The default value of this property is YES
        _searchVC.obscuresBackgroundDuringPresentation = NO;
        
        //_searchVC.hidesNavigationBarDuringPresentation = NO;
        //_searchVC.searchBar.frame = CGRectMake(10, 10, 200, 140);
        
        //UISearchController作为tableview的headerView
        _tvList.tableHeaderView = _searchVC.searchBar;
    }
    
    #pragma mark - UITableViewDelegate, UITableViewDataSource
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        if (_searchVC.active) {
            return self.searchItems.count;
        }
        return self.items.count;
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
        
        if (_searchVC.active) {
            cell.textLabel.text = _searchItems[indexPath.row];
        } else {
            cell.textLabel.text = _items[indexPath.row];
        }
        
        return cell;
    }
    
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    //    if (_searchVC.active) {
    //        _searchVC.active = NO;
    //    }
        
        NSString *str = @"";
        if (_searchVC.active) {
            str = _searchItems[indexPath.row];
            
            //这句会调用代理方法 updateSearchResultsForSearchController 再打印 did end !!!
            _searchVC.active = NO;
            
            //
            //[_searchVC.searchBar resignFirstResponder];
            
            NSLog(@"did end");
        } else {
            str = _items[indexPath.row];
        }
        NSLog(@"%@#", str);
    }
    
    #pragma mark - UISearchControllerDelegate, UISearchResultsUpdating
    
    // These methods are called when automatic presentation or dismissal occurs. They will not be called if you present or dismiss the search controller yourself.
    - (void)willPresentSearchController:(UISearchController *)searchController {
        NSLog(@"%@", NSStringFromSelector(_cmd));
    }
    - (void)didPresentSearchController:(UISearchController *)searchController {
        NSLog(@"%@", NSStringFromSelector(_cmd));
    }
    - (void)willDismissSearchController:(UISearchController *)searchController {
        NSLog(@"%@", NSStringFromSelector(_cmd));
    }
    - (void)didDismissSearchController:(UISearchController *)searchController {
        NSLog(@"%@", NSStringFromSelector(_cmd));
    }
    
    // Called after the search controller's search bar has agreed to begin editing or when 'active' is set to YES. If you choose not to present the controller yourself or do not implement this method, a default presentation is performed on your behalf.
    - (void)presentSearchController:(UISearchController *)searchController {
        NSLog(@"%@", NSStringFromSelector(_cmd));
    }
    
    // Called when the search bar's text or scope has changed or when the search bar becomes first responder.
    - (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
        NSLog(@"%@ %@", NSStringFromSelector(_cmd), searchController.searchBar.text);
    
        NSString *searchText = searchController.searchBar.text;
        
        //输入空🈳️的时候,显示源数据
        if (searchText.length == 0) {
            _searchItems = [NSMutableArray arrayWithArray:_items];
            [_tvList reloadData];
            return;
        }
        
        //搜索
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self contains[c] %@", searchText];
        NSArray *filters = [_items filteredArrayUsingPredicate:predicate];
        
        //修改源
        _searchItems = [NSMutableArray arrayWithArray:filters];
        
        //更新tableView
        [_tvList reloadData];
    }
    
    @end
    

    相关文章

      网友评论

        本文标题:OC UISearchController

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