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

  • 相关阅读:
    P2569 [SCOI2010]股票交易 dp 单调队列优化
    luogu P4516 [JSOI2018]潜入行动
    7.12 NOI模拟赛 积性函数求和 数论基础变换 莫比乌斯反演
    luogu P2607 [ZJOI2008]骑士 tarjan dp
    7.11 NOI模拟赛 graph 生成函数 dp 多项式
    luogu P1973 [NOI2011]NOI 嘉年华 dp
    HTML-01文档
    修改服务器为Root直接登录
    strust2--postman遇到的ognl问题
    python-13-函数
  • 原文地址:https://www.cnblogs.com/lyz0925/p/5649866.html
Copyright © 2011-2022 走看看