iOS8 SearchBar相关

来源:互联网 发布:手机最好个人房源软件 编辑:程序博客网 时间:2024/04/28 04:30

UISearchDisplayController被iOS8废止,取而代之的是 UISearchController

本文转载自: http://my.oschina.net/u/1999967/blog/309511

苹果官方:UISearchController

0.initWithSearchResultsController: 可以为nil,表示使用当前的 viewController。也可以使用自定义的其他viewcontroller。

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

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:代理方法  -(void)updateSearchResultsForSearchController:(UISearchController *)searchController


话不多说,直接上代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
@interface ViewController ()             @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 ViewController             - (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];}


当需要使用Push,现实详细信息等情况的时候,需要将searchBar隐藏。可以将active属性设置为NO。

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
    if ([segue.identifier isEqualToString:@"tablePush"])
    {
        if ([segue.destinationViewController isKindOfClass:[CodePushViewController class]])
        {
            CodePushViewController *destVC = (CodePushViewController *)segue.destinationViewController;
            
            destVC.labelName = [showList objectAtIndex:[[tablesView indexPathForSelectedRow] row]];
            
            searchC.active = NO ;
        }
    }
}

0 0