iOS 用UISearchDisplayController實現尋找功能

來源:互聯網
上載者:User

標籤:style   blog   io   color   os   ar   使用   for   sp   

  UISearchDisplayController是iOS中用於處理搜尋功能的控制器,此控制器需要和UISearchBar結合使用

  範例程式碼如下:

  1 //  2 //  WKRootViewController.m  3 //  表格視圖的搜尋功能  4 //  5 //  Created by student on 14-10-20.  6 //  Copyright (c) 2014年 wukong. All rights reserved.  7 //  8   9 #import "WKRootViewController.h" 10  11 @interface WKRootViewController () 12  13 @property (strong, nonatomic) NSMutableArray* dataSource; 14  15 @property (strong, nonatomic)NSMutableArray* resultArrat; 16  17  18 @end 19  20 @implementation WKRootViewController 21 { 22     //用於載入資料來源的表視圖 23     UITableView *_tableView; 24      25     UISearchBar *_searchBar; 26      27     UISearchDisplayController *_searchControl; 28 } 29 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 30 { 31     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 32     if (self) { 33         // Custom initialization 34     } 35     return self; 36 } 37  38 - (void)viewDidLoad 39 { 40     [super viewDidLoad]; 41      42     [self createUI]; 43     [self createDataSource]; 44     // Do any additional setup after loading the view. 45 } 46  47 - (void)createDataSource 48 { 49     _dataSource = [[NSMutableArray alloc] init]; 50     _resultArrat = [[NSMutableArray alloc] init]; 51     for (int i = ‘A‘; i <= ‘z‘; i++) { 52         NSMutableArray *section = [[NSMutableArray alloc] init]; 53         for (int j = 1; j <= 10; j++) { 54             NSString *str = [NSString stringWithFormat:@"%c-%d", i, j]; 55             [section addObject:str]; 56         } 57         [_dataSource addObject:section]; 58     } 59 } 60  61 #pragma mark- UITableViewDataSource 62  63 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 64 { 65     //判斷當前展示的表格 66     if (tableView != _tableView) 67         return 1; 68     return _dataSource.count; 69 } 70  71 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 72 { 73     if (tableView != _tableView) { 74         return _resultArrat.count; 75     } 76     return [[_dataSource objectAtIndex:section] count]; 77 } 78  79 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 80 { 81     [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"]; 82      83     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; 84     if (tableView != _tableView) { 85         cell.textLabel.text = [_resultArrat objectAtIndex:indexPath.row]; 86     }else{ 87         cell.textLabel.text = [[_dataSource objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]; 88     } 89     return cell; 90 } 91  92 - (void)didReceiveMemoryWarning 93 { 94     [super didReceiveMemoryWarning]; 95     // Dispose of any resources that can be recreated. 96 } 97  98  99 #pragma mark - CreateUI100 - (void)createUI101 {102     _tableView = [[UITableView alloc] initWithFrame:CGRectMake(10, 30, 300, 440) style:UITableViewStylePlain];103     _tableView.delegate = self;104     _tableView.dataSource = self;105     [self.view addSubview:_tableView];106     107     _searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 300, 30)];108     _searchBar.searchBarStyle = UISearchBarStyleMinimal;109     _searchBar.delegate = self;110     [_tableView setTableHeaderView:_searchBar];111     /*112      第一個參數:用於輸入搜尋內容的UISearchBar對象113      第二個參數:提供給我的表格視圖資料來源的控制器對象,這個對象必須是實現了表格的兩個協議114      */115     _searchControl = [[UISearchDisplayController alloc] initWithSearchBar:_searchBar contentsController:self];116 //    _searchControl.searchResultsTableView117 //    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 60, 30)];118 //    label.backgroundColor =[UIColor redColor];119 //    [_searchControl.searchResultsTableView setTableHeaderView:label];120     //設定_searchControl內建的表格視圖的委派物件121     [_searchControl setSearchResultsDataSource:self];122     [_searchControl setSearchResultsDelegate:self];123 }124 125 #pragma mark -UISearchBarDelegate126 - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText127 {128     [_resultArrat removeAllObjects];129     NSString *str = [NSString stringWithFormat:@"*%@*", searchText];130     NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF like %@", str];131     for (NSMutableArray *arr in _dataSource) {132         for (NSString *str in arr) {133             if ([pred evaluateWithObject:str]) {134                 [_resultArrat addObject:str];135             }136         }137     }138 }139 @end

 

iOS 用UISearchDisplayController實現尋找功能

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.