热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

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

效果如下:ViewController.h1#import23@interfaceViewController:UITableViewController4@property(st

效果如下:

技术分享

ViewController.h

1 #import 
2 
3 @interface ViewController : UITableViewController
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) {
 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 
2 
3 @interface AppDelegate : UIResponder 
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在屏幕中实现一个检索框效果


推荐阅读
  • 后台获取视图对应的字符串
    1.帮助类后台获取视图对应的字符串publicclassViewHelper{将View输出为字符串(注:不会执行对应的ac ... [详细]
  • 本文介绍了在SpringBoot中集成thymeleaf前端模版的配置步骤,包括在application.properties配置文件中添加thymeleaf的配置信息,引入thymeleaf的jar包,以及创建PageController并添加index方法。 ... [详细]
  • 深入理解CSS中的margin属性及其应用场景
    本文主要介绍了CSS中的margin属性及其应用场景,包括垂直外边距合并、padding的使用时机、行内替换元素与费替换元素的区别、margin的基线、盒子的物理大小、显示大小、逻辑大小等知识点。通过深入理解这些概念,读者可以更好地掌握margin的用法和原理。同时,文中提供了一些相关的文档和规范供读者参考。 ... [详细]
  • 本文介绍了lua语言中闭包的特性及其在模式匹配、日期处理、编译和模块化等方面的应用。lua中的闭包是严格遵循词法定界的第一类值,函数可以作为变量自由传递,也可以作为参数传递给其他函数。这些特性使得lua语言具有极大的灵活性,为程序开发带来了便利。 ... [详细]
  • 本文介绍了通过ABAP开发往外网发邮件的需求,并提供了配置和代码整理的资料。其中包括了配置SAP邮件服务器的步骤和ABAP写发送邮件代码的过程。通过RZ10配置参数和icm/server_port_1的设定,可以实现向Sap User和外部邮件发送邮件的功能。希望对需要的开发人员有帮助。摘要长度:184字。 ... [详细]
  • 动态规划算法的基本步骤及最长递增子序列问题详解
    本文详细介绍了动态规划算法的基本步骤,包括划分阶段、选择状态、决策和状态转移方程,并以最长递增子序列问题为例进行了详细解析。动态规划算法的有效性依赖于问题本身所具有的最优子结构性质和子问题重叠性质。通过将子问题的解保存在一个表中,在以后尽可能多地利用这些子问题的解,从而提高算法的效率。 ... [详细]
  • Java验证码——kaptcha的使用配置及样式
    本文介绍了如何使用kaptcha库来实现Java验证码的配置和样式设置,包括pom.xml的依赖配置和web.xml中servlet的配置。 ... [详细]
  • 高质量SQL书写的30条建议
    本文提供了30条关于优化SQL的建议,包括避免使用select *,使用具体字段,以及使用limit 1等。这些建议是基于实际开发经验总结出来的,旨在帮助读者优化SQL查询。 ... [详细]
  • 本文介绍了指针的概念以及在函数调用时使用指针作为参数的情况。指针存放的是变量的地址,通过指针可以修改指针所指的变量的值。然而,如果想要修改指针的指向,就需要使用指针的引用。文章还通过一个简单的示例代码解释了指针的引用的使用方法,并思考了在修改指针的指向后,取指针的输出结果。 ... [详细]
  • 猜字母游戏
    猜字母游戏猜字母游戏——设计数据结构猜字母游戏——设计程序结构猜字母游戏——实现字母生成方法猜字母游戏——实现字母检测方法猜字母游戏——实现主方法1猜字母游戏——设计数据结构1.1 ... [详细]
  • CentOS 7部署KVM虚拟化环境之一架构介绍
    本文介绍了CentOS 7部署KVM虚拟化环境的架构,详细解释了虚拟化技术的概念和原理,包括全虚拟化和半虚拟化。同时介绍了虚拟机的概念和虚拟化软件的作用。 ... [详细]
  • 本文介绍了一种解析GRE报文长度的方法,通过分析GRE报文头中的标志位来计算报文长度。具体实现步骤包括获取GRE报文头指针、提取标志位、计算报文长度等。该方法可以帮助用户准确地获取GRE报文的长度信息。 ... [详细]
  • PDF内容编辑的两种小方法,你知道怎么操作吗?
    本文介绍了两种PDF内容编辑的方法:迅捷PDF编辑器和Adobe Acrobat DC。使用迅捷PDF编辑器,用户可以通过选择需要更改的文字内容并设置字体形式、大小和颜色来编辑PDF文件。而使用Adobe Acrobat DC,则可以通过在软件中点击编辑来编辑PDF文件。PDF文件的编辑可以帮助办公人员进行文件内容的修改和定制。 ... [详细]
  • CentOS 6.5安装VMware Tools及共享文件夹显示问题解决方法
    本文介绍了在CentOS 6.5上安装VMware Tools及解决共享文件夹显示问题的方法。包括清空CD/DVD使用的ISO镜像文件、创建挂载目录、改变光驱设备的读写权限等步骤。最后给出了拷贝解压VMware Tools的操作。 ... [详细]
  • Redis底层数据结构之压缩列表的介绍及实现原理
    本文介绍了Redis底层数据结构之压缩列表的概念、实现原理以及使用场景。压缩列表是Redis为了节约内存而开发的一种顺序数据结构,由特殊编码的连续内存块组成。文章详细解释了压缩列表的构成和各个属性的含义,以及如何通过指针来计算表尾节点的地址。压缩列表适用于列表键和哈希键中只包含少量小整数值和短字符串的情况。通过使用压缩列表,可以有效减少内存占用,提升Redis的性能。 ... [详细]
author-avatar
hazouri林_978
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有