码迷,mamicode.com
首页 > 其他好文 > 详细

098在屏幕中实现一个检索框效果

时间:2015-06-16 01:16:49      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:

效果如下:

技术分享

ViewController.h

1 #import <UIKit/UIKit.h>
2 
3 @interface ViewController : UITableViewController<UISearchBarDelegate>
4 @property (strong, nonatomic) UISearchBar *searchBar;
5 @property (strong, nonatomic) NSMutableArray *mArrDataSourceOfTableView;
6 @property (strong, nonatomic) NSMutableArray *mArrDataSourceOfSearchBar;
7 
8 @end

ViewController.m

  1 #import "ViewController.h"
  2 
  3 @interface ViewController ()
  4 - (void)layoutUI;
  5 - (void)loadNavigation;
  6 - (void)loadData;
  7 - (void)updateTableView:(NSString *)searchText;
  8 - (void)barStyleDidPush:(UIBarButtonItem *)sender;
  9 - (void)tintColorDidPush:(UIBarButtonItem *)sender;
 10 @end
 11 
 12 @implementation ViewController
 13 #define kCount 64
 14 
 15 - (void)viewDidLoad {
 16     [super viewDidLoad];
 17     
 18     [self layoutUI];
 19 }
 20 
 21 - (void)didReceiveMemoryWarning {
 22     [super didReceiveMemoryWarning];
 23     // Dispose of any resources that can be recreated.
 24 }
 25 
 26 - (void)viewWillAppear:(BOOL)animated {
 27     [super viewWillAppear:animated];
 28     [self.navigationController setNavigationBarHidden:NO animated:animated];
 29     [self.navigationController setToolbarHidden:NO animated:animated];
 30 }
 31 
 32 #pragma mark - Private Methods
 33 - (void)layoutUI {
 34     [self loadNavigation];
 35     
 36     [self loadData];
 37     
 38     _searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 50)];
 39     _searchBar.delegate = self;
 40     _searchBar.prompt = @"数字查询";
 41     _searchBar.placeholder = @"请输入0-63之间的数字";
 42     _searchBar.keyboardType = UIKeyboardTypeNumberPad;
 43     _searchBar.barStyle = UIBarStyleDefault;
 44     _searchBar.tintColor = [UIColor blackColor];
 45     [_searchBar sizeToFit]; //设置宽高大小自适应
 46     self.tableView.tableHeaderView = _searchBar;
 47 }
 48 
 49 - (void)loadNavigation {
 50     self.navigationItem.title = @"在屏幕中实现一个检索框效果";
 51     UIBarButtonItem *barBtnBarStyle = [[UIBarButtonItem alloc] initWithTitle:@"改变barStyle"
 52                                                                        style:UIBarButtonItemStyleDone
 53                                                                       target:self
 54                                                                       action:@selector(barStyleDidPush:)];
 55     UIBarButtonItem *barBtnTintColor = [[UIBarButtonItem alloc] initWithTitle:@"改变tintColor"
 56                                                                        style:UIBarButtonItemStyleDone
 57                                                                       target:self
 58                                                                       action:@selector(tintColorDidPush:)];
 59     [self setToolbarItems:@[barBtnBarStyle, barBtnTintColor]];
 60 }
 61 
 62 - (void)loadData {
 63     _mArrDataSourceOfTableView = [[NSMutableArray alloc] initWithCapacity:kCount];
 64     _mArrDataSourceOfSearchBar = [[NSMutableArray alloc] initWithCapacity:kCount];
 65     for (NSInteger i=0; i<kCount; i++) {
 66         [_mArrDataSourceOfTableView addObject:[NSString stringWithFormat:@"%ld", (long)i]];
 67         _mArrDataSourceOfSearchBar[i] = _mArrDataSourceOfTableView[i];
 68     }
 69 }
 70 
 71 - (void)updateTableView:(NSString *)searchText {
 72     [_mArrDataSourceOfSearchBar removeAllObjects];
 73     if (searchText.length == 0) {
 74         _mArrDataSourceOfSearchBar = [[NSMutableArray alloc] initWithArray:_mArrDataSourceOfTableView];
 75     } else {
 76         for (NSString *item in _mArrDataSourceOfTableView) {
 77             if ([item hasPrefix:searchText]) {
 78                 [_mArrDataSourceOfSearchBar addObject:item];
 79             }
 80         }
 81     }
 82     //表格视图tableView更新
 83     [self.tableView reloadData];
 84 }
 85 
 86 - (void)barStyleDidPush:(UIBarButtonItem *)sender {
 87     static NSInteger barStyleIndex = 1;
 88     switch (barStyleIndex%2) {
 89         case 0:
 90             _searchBar.barStyle = UIBarStyleDefault;
 91             break;
 92         case 1:
 93             _searchBar.barStyle = UIBarStyleBlack;
 94             break;
 95     }
 96     barStyleIndex++;
 97 }
 98 
 99 - (void)tintColorDidPush:(UIBarButtonItem *)sender {
100     static NSInteger tintColorIndex = 1;
101     switch (tintColorIndex%2) {
102         case 0:
103             _searchBar.tintColor = [UIColor blackColor];
104             break;
105         case 1:
106             _searchBar.tintColor = [UIColor redColor];
107             break;
108     }
109     tintColorIndex++;
110 }
111 
112 #pragma mark - SearchBar
113 - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
114     [self updateTableView:searchText];
115 }
116 
117 - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
118     [self updateTableView:searchBar.text];
119     //隐藏键盘
120     [_searchBar resignFirstResponder];
121 }
122 
123 #pragma mark - TableView
124 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
125     return [_mArrDataSourceOfSearchBar count];
126 }
127 
128 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
129     static NSString *cellIdentifier = @"cellIdentifier";
130     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
131     if (!cell) {
132         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
133     }
134     cell.textLabel.text = _mArrDataSourceOfSearchBar[indexPath.row];
135     return cell;
136 }
137 
138 @end

AppDelegate.h

1 #import <UIKit/UIKit.h>
2 
3 @interface AppDelegate : UIResponder <UIApplicationDelegate>
4 @property (strong, nonatomic) UIWindow *window;
5 @property (strong, nonatomic) UINavigationController *navigationController;
6 
7 @end

AppDelegate.m

 1 #import "AppDelegate.h"
 2 #import "ViewController.h"
 3 
 4 @interface AppDelegate ()
 5 @end
 6 
 7 @implementation AppDelegate
 8 
 9 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
10     _window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
11     ViewController *viewController = [[ViewController alloc] init];
12     _navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
13     _window.rootViewController = _navigationController;
14     //[_window addSubview:_navigationController.view]; //当_window.rootViewController关联时,这一句可有可无
15     [_window makeKeyAndVisible];
16     return YES;
17 }
18 
19 - (void)applicationWillResignActive:(UIApplication *)application {
20 }
21 
22 - (void)applicationDidEnterBackground:(UIApplication *)application {
23 }
24 
25 - (void)applicationWillEnterForeground:(UIApplication *)application {
26 }
27 
28 - (void)applicationDidBecomeActive:(UIApplication *)application {
29 }
30 
31 - (void)applicationWillTerminate:(UIApplication *)application {
32 }
33 
34 @end

 

098在屏幕中实现一个检索框效果

标签:

原文地址:http://www.cnblogs.com/huangjianwu/p/4579488.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!