IOS UI-UISearchController

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了IOS UI-UISearchController相关的知识,希望对你有一定的参考价值。

ViewController.m
  1 //
  2 //  ViewController.m
  3 //  ios_0224_查找联系人
  4 //
  5 //  Created by ma c on 16/2/24.
  6 //  Copyright © 2016年 博文科技. All rights reserved.
  7 //
  8 
  9 #import "ViewController.h"
 10 #import "SearchResultsVC.h"
 11 
 12 @interface ViewController ()<UITableViewDataSource>
 13 
 14 @property (nonatomic, strong) UITableView *tableView;
 15 @property (nonatomic, strong) NSArray *keys;
 16 @property (nonatomic, strong) NSDictionary *dict;
 17 
 18 @property (nonatomic, strong) UISearchController *searchCtr;
 19 
 20 @end
 21 
 22 @implementation ViewController
 23 
 24 - (void)viewDidLoad {
 25     [super viewDidLoad];
 26     [self createUI];
 27     
 28 }
 29 
 30 - (NSArray *)keys
 31 {
 32     if (_keys == nil) {
 33         NSString *path = [[NSBundle mainBundle] pathForResource:@"sortednames" ofType:@"plist"];
 34         self.dict = [NSDictionary dictionaryWithContentsOfFile:path];
 35         _keys = [[self.dict allKeys] sortedArrayUsingSelector:@selector(compare:)];
 36     }
 37     return _keys;
 38 }
 39 
 40 - (NSDictionary *)dict
 41 {
 42     if (_dict == nil) {
 43         NSString *path = [[NSBundle mainBundle] pathForResource:@"sortednames" ofType:@"plist"];
 44         self.dict = [NSDictionary dictionaryWithContentsOfFile:path];
 45     }
 46     return _dict;
 47 }
 48 
 49 - (void)createUI
 50 {
 51     [self setupTableView];
 52     [self setupSearchVC];
 53 }
 54 
 55 - (void)setupTableView
 56 {
 57     self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStyleGrouped];
 58     self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
 59     self.tableView.dataSource = self;
 60     [self.view addSubview:self.tableView];
 61 }
 62 
 63 - (void)setupSearchVC
 64 {
 65     SearchResultsVC *resultVC = [[SearchResultsVC alloc] initWithDict:self.dict keys:self.keys];
 66     self.searchCtr = [[UISearchController alloc] initWithSearchResultsController:resultVC];
 67     
 68     self.searchCtr.searchResultsUpdater = resultVC;
 69 
 70     self.searchCtr.searchBar.placeholder = @"请输入搜索内容";
 71     [self.searchCtr.searchBar sizeToFit];
 72     self.tableView.tableHeaderView = self.searchCtr.searchBar;
 73 }
 74 
 75 #pragma mark - UITableViewDataSource
 76 
 77 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
 78 {
 79     return self.keys.count;
 80 }
 81 
 82 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
 83 {
 84     NSString *key = self.keys[section];
 85     NSArray *value = self.dict[key];
 86     
 87     return [value count];
 88 }
 89 
 90 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 91 {
 92     static NSString *identifier = @"cellIdentifier";
 93     
 94     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
 95     
 96     if (!cell) {
 97         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
 98     }
 99     NSString *key = self.keys[indexPath.section];
100     NSArray *value = self.dict[key];
101 
102     cell.textLabel.text = value[indexPath.row];
103     return cell;
104 }
105 
106 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
107 {
108     return self.keys[section];
109 }
110 - (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView
111 {
112     return self.keys;
113 }
114 
115 @end
SearchResultsVC.m
 1 //
 2 //  SearchResultsVC.m
 3 //  IOS_0224_查找联系人
 4 //
 5 //  Created by ma c on 16/2/24.
 6 //  Copyright © 2016年 博文科技. All rights reserved.
 7 //
 8 
 9 #import "SearchResultsVC.h"
10 
11 @interface SearchResultsVC ()
12 
13 @property (strong, nonatomic) NSDictionary *dict;
14 @property (strong, nonatomic) NSArray *keys;
15 @property (strong, nonatomic) NSMutableArray *searchList;
16 
17 @end
18 
19 @implementation SearchResultsVC
20 
21 - (instancetype)initWithDict:(NSDictionary *)dict keys:(NSArray *)keys {
22     if (self = [super initWithStyle:UITableViewStylePlain]) {
23         self.dict = dict;
24         self.keys = keys;
25         self.searchList = [[NSMutableArray alloc] init];
26     }
27     return self;
28 }
29 
30 - (void)viewDidLoad {
31     [super viewDidLoad];
32 }
33 
34 - (void)updateSearchResultsForSearchController:(UISearchController *)searchController
35 {
36     NSString *searchString = [searchController.searchBar text];
37     NSLog(@"searchString:%@",searchString);
38     [self.searchList removeAllObjects];
39     
40     if (searchString.length > 0) {
41 
42 //        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[c] %@",searchString];
43         NSPredicate *predicate =
44         [NSPredicate predicateWithBlock:^BOOL(id  _Nonnull evaluatedObject, NSDictionary<NSString *,id> * _Nullable bindings) {
45             
46             NSRange range = [evaluatedObject rangeOfString:searchString
47                                         options:NSCaseInsensitiveSearch];
48             
49             return range.location != NSNotFound;
50         }];
51 
52         for (NSString *key in self.keys) {
53             NSArray *matches = [self.dict[key]
54                                 filteredArrayUsingPredicate: predicate];
55             [self.searchList addObjectsFromArray:matches];
56         }
57         [self.tableView reloadData];
58     }
59 }
60 
61 #pragma mark - UITableViewDataSource
62 
63 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
64     return [self.searchList count];
65 }
66 
67 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
68     
69     static NSString *identifier = @"cellIdentifier";
70     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
71     
72     if (!cell) {
73         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
74     }
75     cell.textLabel.text = self.searchList[indexPath.row];
76     return cell;
77 }
78 @end

 

以上是关于IOS UI-UISearchController的主要内容,如果未能解决你的问题,请参考以下文章

iOS 8、iOS 9、iOS 10 和 iOS 11 上的 UITabBar 的高度是多少?

最佳实践。通过支持 iOS 5、iOS 6 和 iOS 7 UI 使 iOS 应用程序通用

iOS 应用程序 - 如何仅为 iOS 8 用户添加 iOS 8 功能,同时仍支持所有 iOS 7 用户

ios手游平台 ios手游推荐 ios手游排行 ios手游模拟器 ios手游盒子

李洪强iOS开发之iOS学习方法收集

iOS开发 适配iOS10