联系客服:400-805-1963

UISearchBar的使用以及下拉列表框的实现
更新:HHH   时间:2023-1-7


       在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. } 

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

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

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

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

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

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

 

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

这样列表框的控制器就实现了。接下来我们就来看看怎么让出现、隐藏。这点我们利用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]; 
  8. } 

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

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

这样一切都搞定了。

 

好了,总结一下:

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

返回开发技术教程...