iOS UISearchController的使用,iosuisearchbar使用

來源:互聯網
上載者:User

iOS UISearchController的使用,iosuisearchbar使用

 

在iOS9中,UISearchDisplayController 已經被UISearchController替代。搜尋方塊是一種常用的控制項。

假設我們要滿足的需求,產生100個“數字+三個隨機字母”,然後搜尋包含某個字母的結果。

那麼,該怎麼做呢?

 

#import "ViewController.h"@interface ViewController ()<UITableViewDelegate,UITableViewDataSource,UISearchControllerDelegate,UISearchResultsUpdating>//tableView@property (strong, nonatomic)  UITableView *tableView;//searchController@property (strong, nonatomic)  UISearchController *searchController;//資料來源@property (strong,nonatomic) NSMutableArray  *dataList;@property (strong,nonatomic) NSMutableArray  *searchList;@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];        _dataList = [NSMutableArray array];    _searchList = [NSMutableArray array];        self.dataList=[NSMutableArray arrayWithCapacity:100];        //產生100個“數字+三個隨機字母”    for (NSInteger i=0; i<100; i++) {        [self.dataList addObject:[NSString stringWithFormat:@"%ld%@",(long)i,[self shuffledAlphabet]]];    }        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 20,[UIScreen  mainScreen].bounds.size.width ,[UIScreen  mainScreen].bounds.size.height)];            _tableView.delegate = self;    _tableView.dataSource = self;    _tableView.separatorStyle = UITableViewCellSelectionStyleNone;        //建立UISearchController    _searchController = [[UISearchController alloc]initWithSearchResultsController:nil];    //設定代理    _searchController.delegate = self;    _searchController.searchResultsUpdater= self;        //設定UISearchController的顯示內容,以下3個屬性預設為YES    //搜尋時,背景變暗色    _searchController.dimsBackgroundDuringPresentation = NO;    //搜尋時,背景變模糊    _searchController.obscuresBackgroundDuringPresentation = 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.0);        // 添加 searchbar 到 headerview    self.tableView.tableHeaderView = _searchController.searchBar;        [self.view addSubview:_tableView];            // Do any additional setup after loading the view, typically from a nib.}//產生3個隨機字母- (NSString *)shuffledAlphabet {        NSMutableArray * shuffledAlphabet = [NSMutableArray arrayWithArray:@[@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",@"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z"]];        NSString *strTest = [[NSString alloc]init];    for (int i=0; i<3; i++) {        int x = arc4random() % 25;        strTest = [NSString stringWithFormat:@"%@%@",strTest,shuffledAlphabet[x]];    }        return strTest;    }//設定地區的行數-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    if (self.searchController.active) {        return [self.searchList count];    }else{        return [self.dataList count];    }}//返回儲存格內容-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    static NSString *flag=@"cell";    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:flag];    if (cell==nil) {        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:flag];    }    if (self.searchController.active) {        [cell.textLabel setText:self.searchList[indexPath.row]];    }    else{        [cell.textLabel setText:self.dataList[indexPath.row]];    }    return cell;}#pragma mark - UISearchControllerDelegate代理//測試UISearchController的執行過程- (void)willPresentSearchController:(UISearchController *)searchController{    NSLog(@"willPresentSearchController");}- (void)didPresentSearchController:(UISearchController *)searchController{    NSLog(@"didPresentSearchController");}- (void)willDismissSearchController:(UISearchController *)searchController{    NSLog(@"willDismissSearchController");}- (void)didDismissSearchController:(UISearchController *)searchController{    NSLog(@"didDismissSearchController");}- (void)presentSearchController:(UISearchController *)searchController{    NSLog(@"presentSearchController");}-(void)updateSearchResultsForSearchController:(UISearchController *)searchController {        NSLog(@"updateSearchResultsForSearchController");    NSString *searchString = [self.searchController.searchBar text];    NSPredicate *preicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[c] %@", searchString];    if (self.searchList!= nil) {        [self.searchList removeAllObjects];    }    //過濾資料    self.searchList= [NSMutableArray arrayWithArray:[_dataList filteredArrayUsingPredicate:preicate]];    //重新整理表格    [self.tableView reloadData];}

 

UISearchController代理方法的執行過程:

UISearchController的使用步驟:1、建立
//建立UISearchController    _searchController = [[UISearchController alloc]initWithSearchResultsController:nil];

 

2、設定代理
    //設定代理    _searchController.delegate = self;    _searchController.searchResultsUpdater= self;

 

3、設定屬性
    //設定UISearchController的顯示內容,以下3個屬性預設為YES    //搜尋時,背景變暗色    _searchController.dimsBackgroundDuringPresentation = NO;    //搜尋時,背景變模糊    _searchController.obscuresBackgroundDuringPresentation = NO;    //隱藏導覽列    _searchController.hidesNavigationBarDuringPresentation = NO;

 

4、實現代理
- (void)willPresentSearchController:(UISearchController *)searchController;- (void)didPresentSearchController:(UISearchController *)searchController;- (void)willDismissSearchController:(UISearchController *)searchController;- (void)didDismissSearchController:(UISearchController *)searchController;- (void)presentSearchController:(UISearchController *)searchController;- (void)updateSearchResultsForSearchController:(UISearchController *)searchController;

 

 

注意點:

1、如果你希望在同一個視圖中顯示搜尋結果,則通過[[UISearchController alloc]initWithSearchResultsController:nil]。但是這是不支援TVOS,請提供TVOS一定要指定結果控制器。

[[UISearchController alloc]initWithSearchResultsController:VC],可以實現指定結果控制器。

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.