137在搜索框中实现下拉列表效果(扩展知识:表格视图数据源为空数据时显示提示信息)

效果如下:

ViewController.h

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

ViewController.m

  1 #import "ViewController.h"
  2
  3 @interface ViewController ()
  4 - (void)layoutUI;
  5 - (void)loadData;
  6 - (void)updateTableView:(NSString *)searchText;
  7 - (void)setDDLHidden:(BOOL)isHidden animated:(BOOL)animated;
  8 @end
  9
 10 @implementation ViewController
 11 #define kRowCount 64
 12
 13 - (void)viewDidLoad {
 14     [super viewDidLoad];
 15
 16     [self layoutUI];
 17 }
 18
 19 - (void)didReceiveMemoryWarning {
 20     [super didReceiveMemoryWarning];
 21     // Dispose of any resources that can be recreated.
 22 }
 23
 24 #pragma mark - Private Methods
 25 - (void)layoutUI {
 26     [self loadData];
 27
 28     self.navigationItem.title = @"在搜索框中实现下拉列表效果";
 29
 30     _searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 50)];
 31     _searchBar.delegate = self;
 32     _searchBar.prompt = @"数字查询";
 33     _searchBar.placeholder = @"请输入0-63之间的数字";
 34     _searchBar.keyboardType = UIKeyboardTypeNumberPad;
 35     _searchBar.barStyle = UIBarStyleDefault;
 36     _searchBar.tintColor = [UIColor blackColor];
 37     [_searchBar sizeToFit]; //设置宽高大小自适应
 38     self.tableView.tableHeaderView = _searchBar;
 39
 40     _ddlViewController = [[DropDownListViewController alloc] initWithStyle:UITableViewStylePlain];
 41     _ddlViewController.delegate = self;
 42     _ddlViewController.view.frame = CGRectMake(22.0, 68.0, 320.0, 0);
 43     [self.view addSubview:_ddlViewController.view];
 44
 45     _lblEmptyDataMsg = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 50)];
 46     CGPoint newPoint = self.view.center;
 47     newPoint.y -= 25;
 48     _lblEmptyDataMsg.center = newPoint;
 49     _lblEmptyDataMsg.text = @"没有相关数据";
 50     _lblEmptyDataMsg.textColor = [UIColor grayColor];
 51     _lblEmptyDataMsg.textAlignment = NSTextAlignmentCenter;
 52     _lblEmptyDataMsg.font = [UIFont systemFontOfSize:20];
 53     _lblEmptyDataMsg.hidden = YES;
 54     [self.view addSubview:_lblEmptyDataMsg];
 55 }
 56
 57 - (void)viewDidAppear:(BOOL)animated {
 58     [super viewDidAppear:animated];
 59
 60     [self setDDLHidden:YES animated:NO];
 61 }
 62
 63 - (void)loadData {
 64     _mArrDataSourceOfTableView = [[NSMutableArray alloc] initWithCapacity:kRowCount];
 65     _mArrDataSourceOfSearchBar = [[NSMutableArray alloc] initWithCapacity:kRowCount];
 66     for (NSInteger i=0; i<kRowCount; i++) {
 67         [_mArrDataSourceOfTableView addObject:[NSString stringWithFormat:@"%ld", (long)i]];
 68         _mArrDataSourceOfSearchBar[i] = _mArrDataSourceOfTableView[i];
 69     }
 70 }
 71
 72 - (void)updateTableView:(NSString *)searchText {
 73     [_mArrDataSourceOfSearchBar removeAllObjects];
 74     if (searchText.length == 0) {
 75         _mArrDataSourceOfSearchBar = [[NSMutableArray alloc] initWithArray:_mArrDataSourceOfTableView];
 76     } else {
 77         for (NSString *item in _mArrDataSourceOfTableView) {
 78             if ([item hasPrefix:searchText]) {
 79                 [_mArrDataSourceOfSearchBar addObject:item];
 80             }
 81         }
 82     }
 83     //表格视图tableView更新
 84     [self.tableView reloadData];
 85
 86     //设置当UITableView数据源为空数据时,显示提示信息标签_lblEmptyDataMsg
 87     _lblEmptyDataMsg.hidden = _mArrDataSourceOfSearchBar.count > 0;
 88 }
 89
 90 - (void)setDDLHidden:(BOOL)isHidden animated:(BOOL)animated {
 91     NSInteger ddlHeight = isHidden ? 0 : 180;
 92     //声明并实现block代码块setHeightForDDL
 93     void (^setHeightForDDL)() = ^() {
 94         [_ddlViewController.view setFrame:CGRectMake(22.0, 68.0, 320.0, ddlHeight)];
 95     };
 96
 97     if (animated) {
 98         //设置视图变化使用动画效果
 99         [UIView beginAnimations:nil context:nil];
100         [UIView setAnimationDuration:0.3];
101         setHeightForDDL();
102         [UIView commitAnimations];
103     } else {
104         setHeightForDDL();
105     }
106
107 }
108
109 #pragma mark - SearchBar
110 - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
111     //必须设置为最顶层;否则在UITableView数据源为空数据后切换,会出现_ddlViewController.view不是最顶层的现象
112     [self.view bringSubviewToFront:_ddlViewController.view];
113
114     _ddlViewController.searchText = searchText;
115     [self setDDLHidden:(searchText.length == 0) animated:YES];
116     [_ddlViewController updateData];
117
118 }
119
120 - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
121     //隐藏自定义的下拉列表视图
122     [self setDDLHidden:YES animated:YES];
123
124     [self updateTableView:searchBar.text];
125     //隐藏键盘
126     [_searchBar resignFirstResponder];
127 }
128
129 #pragma mark - TableView
130 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
131     return [_mArrDataSourceOfSearchBar count];
132 }
133
134 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
135     static NSString *cellIdentifier = @"cellIdentifier";
136     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
137     if (!cell) {
138         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
139     }
140     cell.textLabel.text = _mArrDataSourceOfSearchBar[indexPath.row];
141     return cell;
142 }
143
144 #pragma PassValueDelegate
145 - (void)passValue:(NSString *)value {
146     if (value) {
147         _searchBar.text = value;
148         [self searchBarSearchButtonClicked:_searchBar];
149     }
150 }
151
152 @end

PassValueDelegate.h

1 @protocol PassValueDelegate <NSObject>
2 - (void)passValue:(NSString *)value;
3
4 @end

DropDownListViewController.h

 1 #import <UIKit/UIKit.h>
 2 #import "PassValueDelegate.h"
 3
 4 @interface DropDownListViewController : UITableViewController
 5 {
 6     @private
 7     NSString *_selectedText;
 8 }
 9 @property (strong, nonatomic) NSMutableArray *mArrResultList;
10 @property (copy, nonatomic) NSString *searchText;
11 @property (weak, nonatomic) id<PassValueDelegate> delegate;
12
13 - (void)updateData;
14
15 @end

DropDownListViewController.m

 1 #import "DropDownListViewController.h"
 2
 3 @interface DropDownListViewController ()
 4 - (void)layoutUI;
 5 @end
 6
 7 @implementation DropDownListViewController
 8 #define kRowCount 6
 9 #define kHeightForRow 30.0
10
11 - (void)viewDidLoad {
12     [super viewDidLoad];
13
14     [self layoutUI];
15 }
16
17 - (void)didReceiveMemoryWarning {
18     [super didReceiveMemoryWarning];
19     // Dispose of any resources that can be recreated.
20 }
21
22 - (void)layoutUI {
23     self.tableView.layer.borderColor = [UIColor blackColor].CGColor;
24     self.tableView.layer.borderWidth = 1.0;
25
26     _mArrResultList = [[NSMutableArray alloc] initWithCapacity:kRowCount];
27     _searchText = nil;
28     _selectedText = nil;
29 }
30
31 - (void)updateData {
32     [_mArrResultList removeAllObjects];
33     if (_searchText && _searchText.length > 0) {
34         [_mArrResultList addObject:_searchText];
35         for (NSInteger i=1; i<kRowCount; i++) {
36             [_mArrResultList addObject:[NSString stringWithFormat:@"%@%ld", _searchText, (long)i]];
37         }
38     }
39
40     [self.tableView reloadData];
41 }
42
43 #pragma mark - TableView
44 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
45     return nil;
46 }
47
48 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
49     return 1;
50 }
51
52 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
53     return (_searchText && _searchText.length > 0) ? kRowCount : 0;
54 }
55
56 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
57     static NSString *cellIdentifier = @"cellIdentifier";
58     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
59     if (!cell) {
60         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
61     }
62     cell.textLabel.text = _mArrResultList[indexPath.row];
63     return cell;
64 }
65
66 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
67     _selectedText = _mArrResultList[indexPath.row];
68     [_delegate passValue:_selectedText];
69 }
70
71 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
72     return kHeightForRow;
73 }
74
75 @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-12-19 22:01:11

137在搜索框中实现下拉列表效果(扩展知识:表格视图数据源为空数据时显示提示信息)的相关文章

网页中制作搜索框中遇到一些小问题

当你在制作一个搜索框加按钮的时候,比如<input class="header-text"/><input type="button" class="header-button"/>这样放置,文本框和按钮不管你怎么调样式也不在同一个位置,这个时候解决方式有两种: 1.外边包一层div,让文本框和按钮float:left,此时文本框和按钮都会紧贴div,也就会出现在同一水平线上的效果 2.待搜索

vue搜索框中如何根据子类找到父类,并把子类和父类作为参数传递

问题: 最近做vue项目,需要做到搜索框,搜素的值是子类,跳转的时候还要带上子类的父类作为参数,这本来就是很简单的事情,技术菜,折腾了好久,请教大佬才瞬间明白,多么简单的事.故此记录 1 后台返回格式是 2 搜索框效果  3 如何通过点击子类获取父类并作为参数调转?下面贴出代码 给li注册点击事件并把子类的值拿到 goSearch(item) { let parent for(const key in this.objtest){ if(this.objtest[key].includes(it

javascript实现组合列表框中元素移动效果

应用背景:在页面中有两个列表框,需要把其中一个列表框的元素移动到另一个列表框 .  实现的基本思想: (1)编写init方法对两个列表框进行初始化: (2)为body添加onload事件调用init方法: (3)编写move(s1,s2)把s1中选中的选项移到s2; (4)编写moveAll(s1,s2)把s1中所有的选项都移到s2. (5)为按钮添加onclick事件. javascript代码如下: 1 <script type="text/javascript" langu

搜索框中“请输入搜索keyword”

$(function(){    $("#ctl00_txtKey").val("请输入搜索keyword").addClass("search")    .blur(function(){        $(this).removeClass('highligth');        if($(this).val()==""){            $("#ctl00_txtKey").val(&quo

weui 弹框中的单选效果

<!--性别修改弹框--> <div class="weui_dialog_alert" id="doctorSexDialog" style="display: none;"> <div class="weui_mask"></div> <div class="weui_dialog"> <div class="weui_dial

bootstrap搜索框样式代码及效果

<div class="container"> <div class="input-group"> <input type="text" class="form-control input-lg"><span class="input-group-addon btn btn-primary">搜索</span> </div> </

如何修改搜索框中的cancel button的颜色

// 记住最初的状态,用完的时候用这个还原 self.cancelBtnTextAttribute = UIBarButtonItem.appearance().titleTextAttributes(for: UIControlState.normal) as [String : AnyObject]? let cancelBtn:UIButton = self.searchBar.subviews[0].subviews[2] as! UIButton // 设置自定义的状态 cancelB

Spring MVC中传递json数据时显示415错误解决方法

在ajax中设置 ContentType为'application/json;charset=utf-8' 传递的data类型必须是json字符串类型:{“key”:"value"}; 并且一定要指定 produces = "application/json" @RequestMapping(value="/register_cammmend",method = RequestMethod.POST,produces = "applic

iOS中UISearchBar(搜索框)使用总结

iOS中UISearchBar(搜索框)使用总结 初始化:UISearchBar继承于UIView,我们可以像创建View那样创建searchBar     UISearchBar * bar = [[UISearchBar alloc]initWithFrame:CGRectMake(20, 100, 250, 40)];     [self.view addSubview:bar]; @property(nonatomic)        UIBarStyle              ba