搜尋方塊UISearchController的使用(iOS8.0以後替代UISearchBar + UISearchDisplayController)

來源:互聯網
上載者:User

標籤:

1.searchResultsUpdater:設定顯示搜尋結果的控制器

?
1     _mySearchController.searchResultsUpdater = self;

 

2.dimsBackgroundDuringPresentation:設定開始搜尋時背景顯示與否

?
1     _mySearchController.dimsBackgroundDuringPresentation = NO;

 

3.[searchBar sizeToFit]:設定searchBar位置自適應

?
1     [_mySearchController.searchBar sizeToFit];

 

4.設定searchBar為UITableView的頭部視圖

?
1     self.myTableView.tableHeaderView = self.mySearchController.searchBar;

 

5.UISearchResultsUpdating:代理方法

#import "SearchViewController.h"

@interface ShareViewController ()<UISearchResultsUpdating,UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UITableView *myTableView;

@property (nonatomic, strong) NSMutableArray *visableArray;//可見的

@property (nonatomic, strong) NSMutableArray *filterArray;//濾波器

@property (nonatomic, strong) NSMutableArray *dataSourceArray;

@property (nonatomic, strong) UISearchController *mySearchController;

 

@end

 

@implementation SearchViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    

    [self initial];

}

 

- (void)initial{

    self.dataSourceArray = [NSMutableArray array];

    self.filterArray = [NSMutableArray array];

    for (int i = 0; i < 26; i++) {

        for (int j = 0; j < 4; j++) {

            NSString *str = [NSString stringWithFormat:@"%c%d", ‘A‘+i, j];

            [self.dataSourceArray addObject:str];

        }

    }

    

    self.visableArray = self.dataSourceArray;

    

    self.myTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];

    _myTableView.delegate = self;

    _myTableView.dataSource = self;

    [self.view addSubview:_myTableView];

    

    self.mySearchController = [[UISearchController alloc] initWithSearchResultsController:nil];

    _mySearchController.searchResultsUpdater = self;

    _mySearchController.dimsBackgroundDuringPresentation = NO;

    [_mySearchController.searchBar sizeToFit];

    

    self.myTableView.tableHeaderView = self.mySearchController.searchBar;

}

 

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

    if (!_visableArray || _visableArray.count == 0) {

        _visableArray = _dataSourceArray;

    }

    return _visableArray.count;

}

 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"identifier"];

    

    if (!cell) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"identifier"];

    }

    

    cell.textLabel.text = [_visableArray objectAtIndex:indexPath.row];

    

    return cell;

}

 

- (void)updateSearchResultsForSearchController:(UISearchController *)searchController{

    NSString *filterString = searchController.searchBar.text;

    

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains [c] %@", filterString];

    

    self.visableArray = [NSMutableArray arrayWithArray:[self.dataSourceArray filteredArrayUsingPredicate:predicate]];

    

    [self.myTableView reloadData];

}

 

搜尋方塊UISearchController的使用(iOS8.0以後替代UISearchBar + 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.