温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

UISearchBar的使用以及下拉列表框的实现

发布时间:2020-06-14 11:18:15 来源:网络 阅读:26719 作者:izhuhaoDev 栏目:开发技术

       在IOS混饭吃的同志们都很清楚,搜索框在移动开发应用中的地位。今天我们就结合下拉列表框的实现来聊聊UISearchBar的使用。本人新入行的菜鸟一个,不足之处请多多指教。直接上代码。

UISearchBar控件的声明:(在控制器DownListViewController中)

  1. @property (nonatomic,retain) UISearchBar* searchBar; 

控件的初始化:

  1. _searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, 320, 40)]; 
  2. _searchBar.placeholder = @"test";   //设置占位符 
  3. _searchBar.delegate = self;   //设置控件代理 

当然,做完这些工作之后,我们还要在将控件添加到父视图之上,也可以把他设置成UITableView的tableHeaderView属性值,由于大家需求不一,这里就不再给出代码。

前面,我们设置了控件的代理,当然我们必须让控制器(DownListViewController)的 .h 文件实现 UISearchBarDelegate 协议,然后我们继续, 我们要在 .m 文件中实现协议方法:

  1. #pragma mark - 
  2. #pragma mark UISearchBarDelegate 
  3.  
  4. //搜索框中的内容发生改变时 回调(即要搜索的内容改变)
  5. - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{ 
  6.     NSLog(@"changed"); 
  7.     if (_searchBar.text.length == 0) { 
  8.         [self setSearchControllerHidden:YES]; //控制下拉列表的隐现
  9.     }else{ 
  10.         [self setSearchControllerHidden:NO]; 
  11.   
  12.     } 

  13. - (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar { 
  14.     searchBar.showsCancelButton = YES
  15. for(id cc in [searchBar subviews])
    {
    if([cc isKindOfClass:[UIButton class]])
    {
    UIButton *btn = (UIButton *)cc;
    [btn setTitle:@"取消" forState:UIControlStateNormal];
    }
    }
  16.     NSLog(@"shuould begin"); 
  17.     return YES; 
  18.  
  19. - (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar { 
  20.     searchBar.text = @""; 
  21.     NSLog(@"did begin"); 
  22.  
  23. - (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar { 
  24.     NSLog(@"did end"); 
  25.     searchBar.showsCancelButton = NO
  26.  
  27.  
  28. - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar { 
  29.     NSLog(@"search clicked"); 
  30.  
  31. //点击搜索框上的 取消按钮时 调用
  32. - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar { 
  33.     NSLog(@"cancle clicked"); 
  34.     _searchBar.text = @""; 
  35.     [_searchBar resignFirstResponder]; 
  36.     [self setSearchControllerHidden:YES]; 

至此,搜索框的实现就搞定了,怎么样简单吧。下面我们来讲讲下拉列表框的实现,先说说他的实现原理或者是思路吧。下拉列表框我们用一个控制器来实现,我们新建一个控制器SearchViewController.

  1. @interface SearchViewController : UITableViewController 
  2.  
  3. @end 

在 .m 文件中,我们实现该控制器

  1. - (id)initWithStyle:(UITableViewStyle)style 
  2.     self = [super initWithStyle:style]; 
  3.     if (self) { 
  4.         // Custom initialization 
  5.     } 
  6.     return self; 
  7.  
  8. - (void)viewDidLoad 
  9.     [super viewDidLoad]; 
  10.  
  11.     self.tableView.layer.borderWidth = 1
  12.     self.tableView.layer.borderColor = [[UIColor blackColor] CGColor]; 
  13.  

然后实现控制器的数据源,

 

  1. #pragma mark - 
  2. #pragma mark Table view data source 
  3.  
  4. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
  5.     return 1; 
  6.  
  7.  
  8. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
  9.     // 返回列表框的下拉列表的数量
  10.     return 3; 
  11.  
  12.  
  13. // Customize the appearance of table view cells. 
  14. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
  15.      
  16.     static NSString *CellIdentifier = @"Cell"; 
  17.      
  18.     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
  19.     if (cell == nil) { 
  20.         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ; 
  21.     } 
  22.      
  23.     // Configure the cell... 
  24.     NSUInteger row = [indexPath row]; 
  25.     cell.textLabel.text = @"down list"; 
  26.      
  27.     return cell; 

这样列表框的控制器就实现了。接下来我们就来看看怎么让出现、隐藏。这点我们利用UIView的动画效果来实现,我们在DownListViewController控制器中 增加一个方法:

  1. - (void) setSearchControllerHidden:(BOOL)hidden { 
  2.     NSInteger height = hidden ? 0: 180; 
  3.     [UIView beginAnimations:nil context:nil]; 
  4.     [UIView setAnimationDuration:0.2]; 
  5.      
  6.     [_searchController.view setFrame:CGRectMake(30, 36, 200, height)]; 
  7.     [UIView commitAnimations]; 

我们只需调用该方法就可以了。现在我们看看DownListViewController的布局方法

  1. - (void)viewDidLoad 
  2.     [super viewDidLoad]; 
  3.     _searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, 320, 40)]; 
  4.     _searchBar.placeholder = @"test"; 
  5.     _searchBar.delegate = self;  
  6.      
  7.     _tableview = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain]; 
  8.     _tableview.dataSource = self
  9.     _tableview.tableHeaderView = _searchBar
  10.      
  11.     _searchController = [[SearchViewController alloc] initWithStyle:UITableViewStylePlain]; 
  12.     [_searchController.view setFrame:CGRectMake(30, 40, 200, 0)]; 
  13.      
  14.     [self.view addSubview:_tableview]; 
  15.     [self.view addSubview:_searchController.view]; 

这样一切都搞定了。

 

UISearchBar的使用以及下拉列表框的实现

好了,总结一下:

我们用了两个控制器:DownListViewController(搜索框的实现 和 控制下拉列表框的出现与隐藏)和SearchViewController(下拉列表框的实现)。在DownListViewController中我们声明并初始化 UISearchBar和SearchViewController(高度开始设置为零),用动画来实现下拉列表框的出现与隐藏。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI