zoukankan      html  css  js  c++  java
  • UISearchController的使用

    - (void)addSearchController

    {

        _searchController = [[UISearchController alloc] initWithSearchResultsController:nil];

        _searchController.delegate = self;

        _searchController.searchResultsUpdater = self;

        

        // 必须要让searchBar自适应才会显示

        [_searchController.searchBar sizeToFit];

        _searchController.searchBar.delegate = self;

        [_searchController.searchBar setAutocapitalizationType:UITextAutocapitalizationTypeNone];

        _searchController.searchBar.backgroundImage = [UIImage imageWithColor:kAppBakgroundColor];

        _searchController.searchBar.placeholder = @"用户ID/昵称";

        _searchController.hidesNavigationBarDuringPresentation = NO;

        _searchController.dimsBackgroundDuringPresentation = NO;

        self.definesPresentationContext = NO;

        //把searchBar 作为 tableView的头视图

        self.tableView.tableHeaderView = _searchController.searchBar;

        

    }

        //把searchBar 作为 tableView的头视图

        self.tableView.tableHeaderView = _searchController.searchBar;

    #pragma mark - UISearchController的代理

    - (void)updateSearchResultsForSearchController:(UISearchController *)searchController

    {

        if (self.searchResults.count) {

            [self.searchResults removeAllObjects];

        }

        

        NSPredicate *searchPredicate = [NSPredicate predicateWithFormat:@"userId CONTAINS[c] %@",searchController.searchBar.text];

        NSArray *array = [(NSArray *)self.searchArray filteredArrayUsingPredicate:searchPredicate];

        self.searchResults = [NSMutableArray arrayWithArray:array];

        //刷新表格

        [self.tableView reloadData];

        

    }

    在tableView中使用时,可以根据_searchController.active的属性来判断是否显示搜索到的数据

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

    {

        if (_searchController.active) {

            return [self.searchResults count];

        }else{

            id<IMAContactDrawerShowAble> drawer = [_contact objectAtIndex:section];

            if ([self isDrawerFolded:drawer])

            {

                return 0;

            }

            else

            {

                return [drawer items].count;

            }

        }

       

    }

     参考地址:http://www.mamicode.com/info-detail-1156195.html

                   http://www.2cto.com/kf/201510/447287.html

  • 相关阅读:
    vs2017中信号与槽连接
    生成格雷码
    结构光三维测量技术
    格雷码生成算法
    结构光三维重建
    Qt之CMake和MinGW编译OpenCV
    qt+opencv编译环境的配置
    vs2017+opencv配置参考链接
    2019-3-25多线程的同步与互斥(互斥锁、条件变量、读写锁、自旋锁、信号量)
    2019-3-22c# TextBox只允许输入数字,禁用右键粘贴,允许Ctrl+v粘贴数字
  • 原文地址:https://www.cnblogs.com/lyz0925/p/5649866.html
Copyright © 2011-2022 走看看