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

效果如下:

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
时间: 2024-08-02 10:57:29

098在屏幕中实现一个检索框效果的相关文章

179在屏幕中绘制一个三角形

效果如下: ViewController.h 1 #import <UIKit/UIKit.h> 2 3 @interface ViewController : UIViewController 4 @end ViewController.m 1 #import "ViewController.h" 2 #import "KMTriangleView.h" 3 4 @interface ViewController () 5 - (void)layout

034在屏幕中显示一个工具条

效果如下: ViewController.h 1 #import <UIKit/UIKit.h> 2 3 @interface ViewController : UIViewController 4 @end ViewController.m 1 #import "ViewController.h" 2 3 @interface ViewController () 4 - (void)nextDidPush; 5 @end 6 7 @implementation ViewC

059在屏幕中显示一个文本输入框

效果如下: ViewController.h 1 #import <UIKit/UIKit.h> 2 3 @interface ViewController : UIViewController 4 @property (strong, nonatomic) UITextField *txtFMessage; 5 6 @end ViewController.m 1 #import "ViewController.h" 2 3 @interface ViewControlle

uwp开发:截取当前屏幕中需要的图像并保存至应用内存储

在uwp开发中,有时候需要获取当前屏幕中的图像信息,但是又不适合直接截图保存,因为截图会保存整个屏幕的图像,而且,还需要用户会截屏操作.总之不适合获取屏幕中需要的图像信息.注意题目中的“需要的”. 意思是什么呢?就是我们可以获取当前屏幕中任意一个UIElement中的图像.废话不多说,还是以实战场景为例,因为自己最近就遇到了这种情况. 在做<简影UWP>的“电影台词”模块的时候,显示如下: 需求是:是用户点击保存图片,将会把图片和文字一块保存下来,查看的时候,也是当前显示的这样. 首先贴上前台

101在检索框中添加一个书签按钮(扩展知识:在检索框中添加一个范围条)

效果如下: ViewController.h 1 #import <UIKit/UIKit.h> 2 3 @interface ViewController : UITableViewController<UISearchBarDelegate> 4 @property (strong, nonatomic) UISearchBar *searchBar; 5 @property (strong, nonatomic) NSMutableArray *mArrDataSourceO

如果exclipe中Java视图中Consol跑偏了单独形成一个弹框怎么办?

问题如图: 不和其他的在同一窗体中,而且拖动也拖不进去,则是使用以下方法: 点击工具栏上的Window--->点击Reset Perspective...这样就可以解决了. 如果exclipe中Java视图中Consol跑偏了单独形成一个弹框怎么办?,布布扣,bubuko.com

下拉框&mdash;&mdash;把一个select框中选中内容移到另一个select框中遇到的问题

在使用jQuery实现把一个select框中选中内容移到另一个select框中功能时遇到了一个问题,就是点击按钮时内容可以到另一个select框中,但是到了另一个select框中的内容却很快闪退回原来的select框中,代码如下:                  <select class="select1" name="select1" multiple="multiple">                     <op

javascript 获取光标所选中的内容并插入到另一个文本框中(兼容ie和ff)

项目中正好用到 做下笔记方便以后查找 ie获取光标的位置使用document.selection.createRange() 火狐下使用document.getElementById(id).selectionStart 和 document.getElementById(id).selectionEnd 假设我们要获得id为txt的Textarea元素里面光标所选择的内容 首先创建一个获得光标所选内容的函数(参数:火狐下需要Dom元素 select_field=document.getElem

点击事件中实现弹出一个选择框(如选择网络设置、选择电话短信联系方式)

1.网络设置 public void checkNetwork(){ //获取连接的管理对象 ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); //获取当前正在使用的网络 NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo(); //判断网络是