分享

【学习ios之路:UI系列】(UISearchBar,UISearchDisplayController) 和UISearchController(iOS8新特性)

 叹落花 2015-06-15

1.UISearchBar(效果如下:)

【学习ios之路:UI系列】(UISearchBar,UISearchDisplayController) 和UISearchController(iOS8新特性)0

①创建UISearchBar对象

<span style="font-size:18px;">    //初始化,定义frame
    UISearchBar *bar = [[UISearchBar alloc] initWithFrame:CGRectMake
                                            (0, 50, self.view.frame.size.width, 80)];
    //添加到控制器的视图上 
    [self.view addSubview:bar];</span>
②UISerachBar的属性

<span style="font-size:18px;">   //autocapitalizationType:包含4种类型,但是有时候键盘会屏蔽此属.
  //1.autocapitalizationType????自动对输入文本对象进行大小写设置.
    bar.autocapitalizationType = UITextAutocapitalizationTypeWords;
  //2.autocorrectionType????自动对输入文本对象进行纠错 
   bar.autocorrectionType = UITextAutocorrectionTypeYes; 
  //3.设置title
   bar.prompt = @"全部联系人"; 

   //4.设置颜色 
   bar.tintColor  = [UIColor purpleColor];//渲染颜色
   bar.barTintColor = [UIColor orangeColor];//搜索条颜色
   bar.backgroundColor =  [UIColor purpleColor];//背景颜色,因为毛玻璃效果(transulent).

   //5.translucent????指定控件是否会有透视效果
   bar.translucent = YES; 

   //6.scopeButtonTitles(范围buttonTitle)
    bar.scopeButtonTitles = @[@"精确搜索",@"模糊搜索"];
    bar.selectedScopeButtonIndex = 1;//通过下标指定默认选择的那个选择栏

   //7.控制搜索栏下部的选择栏是否显示出来(需设置为YES 才能使用scopebar)
    bar.showScopeBar = YES;

   //8.设置搜索栏右边的按钮
    bar.showsSearchResultsButton  = YES;//向下的箭头
    bar.showsCancelButton = YES; //取消按钮
    bar.showsBookmarkButton =  YES; //书签按钮

    //9.提示内容
    bar.placeholder = @"搜索";

    //10.取消键盘操作
   [searchBar resignFirstResponder]; 

   //11.设置代理
   //UISearchBar不执行搜索行为,必须使用delegate,当输入搜索文本、点击button按钮后,代理的方法
    会完成搜索对应的操作。
   //.控件的委托,委托要遵从UISearchBarDelegate协议,默认是nil
   bar.delegate = self;
</span>

③代理要试实现的协议方法

1).输入编辑事件处理

<span style="font-size:18px;">? searchBar:textDidChange:

? searchBar:shouldChangeTextInRange:replacementText:

? searchBarShouldBeginEditing:

? searchBarTextDidBeginEditing:

? searchBarShouldEndEditing:

? searchBarTextDidEndEditing:</span>
2).点击按钮事件处理
<span style="font-size:18px;">? searchBarBookmarkButtonClicked:

? searchBarCancelButtonClicked:

? searchBarSearchButtonClicked:

? searchBarResultsListButtonClicked:</span>
3).点击Scope按钮事件处理
<span style="font-size:18px;">? searchBar:selectedScopeButtonIndexDidChange:</span>

2.UISearchDisplayController(注:iOS8以上已经弃用)

结合UISearchBar实现效果如下,实现搜索功能.

【学习ios之路:UI系列】(UISearchBar,UISearchDisplayController) 和UISearchController(iOS8新特性)1

提示:检测Xcode系统版本代码如下:

<span style="font-size:18px;">[[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0 ? YES: NO;</span>
①.创建对象
<span style="font-size:18px;">    //需要创建UISearchBar对象,这里将对象都定义成了属性
    self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
    
    //设置UISearchBar属性,上面有UISearchBar详细介绍.
    self.searchBar.placeholder = @"enter province name";
    self.searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
   self.searchBar.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters;
    self.searchBar.scopeButtonTitles = [NSArray arrayWithObjects:
<span style="color:#FF0000;">                                                   </span>@"All",@"A",@"B",@"C",@"D" ,nil];
    self.searchBar.showsScopeBar = YES;
    self.searchBar.keyboardType = UIKeyboardTypeNamePhonePad;
    self.searchBar.showsBookmarkButton = YES;
   
    //将seachBar作为控制器的透视图,视图控制器,继承UITableViewController
    self.tableView.tableHeaderView = _searchBar;

    //将UIsearchBar添加到UIdSearchDispalyController上 
    self.displayController = [[UISearchDisplayController alloc]
                              initWithSearchBar:_searchBar contentsController:self];</span>
注:searchBar????在searchdisplaycontroller初始化后,searchbar是不可修改的,是readonly属性的.

②配置UISearchDisplayController的属性

<span style="font-size:18px;">   //active????是搜索界面可视化,默认为no,可用setActive方法设置.
    self.displayController.active = YES;

    // searchResultsDataSource????搜索结果的数据源,代理对象(UITableViewDataSource)
    self.displayController.searchResultsDataSource = self;

    //searchResultsDelegate????搜索结果的委托,服从协议UITableViewDelegate   
    self.displayController.searchResultsDelegate = self;//
</span>
③实现
<span style="font-size:18px;">   /*searchDisplayController 自身有一个searchResultsTableView,
     所以在执行操作的时候首先要判断是否是搜索结果的tableView,如果是显示的就是搜索结果的数据,
    如果不是,是TableView自身的view,则需要显示原始数据。*/
    if (tableView == self.tableView) {
        return self.dataArray.count;
    } else {
    
        NSPredicate *predicate = [NSPredicate predicateWithFormat:
                                      @"self contains [cd] %@",self.searchBar.text];
        self.arr  =  [[NSMutableArray alloc] initWithArray:
                            [self.dataArray filteredArrayUsingPredicate:predicate]];
        return self.arr.count;
        
    }</span>

④使用UISearchDisplayDelegate的委托方法进行搜索操作:


1).搜索状态改变事件处理方法:

<span style="font-size:18px;">? searchDisplayControllerWillBeginSearch:

? searchDisplayControllerDidBeginSearch:

? searchDisplayControllerWillEndSearch:

? searchDisplayControllerDidEndSearch:
</span>


2).装载和卸载tableview事件处理方法:

<span style="font-size:18px;">? searchDisplayController:didLoadSearchResultsTableView:

? searchDisplayController:willUnloadSearchResultsTableView:</span>


3).显示和隐藏tableview事件处理方法:

<span style="font-size:18px;">? searchDisplayController:willShowSearchResultsTableView:

? searchDisplayController:didShowSearchResultsTableView:

? searchDisplayController:willHideSearchResultsTableView:

? searchDisplayController:didHideSearchResultsTableView:</span>
4).搜索条件改变时响应事件处理方法:
<span style="font-size:18px;">? searchDisplayController:shouldReloadTableForSearchString:

? searchDisplayController:shouldReloadTableForSearchScope</span>

3.UISearchController(iOS8新特性)

UISearchController实现和上述效果基本一致,适用于iOS8以上版本

实现如下图搜索效果

【学习ios之路:UI系列】(UISearchBar,UISearchDisplayController) 和UISearchController(iOS8新特性)2

代码如下:

1)新建控制器,继承与UITableViewController,在extension中定义属性

<span style="font-size:18px;">//存储原来的数据
@property (nonatomic, retain) NSArray *dataArr;
//存储检索后的数据
@property (nonatomic, retain) NSArray *arr;

</span>
2).加载数据,懒加载
<span style="font-size:18px;">- (NSArray *)dataArr {
    if (!_dataArr) {
    self.dataArr = [NSArray arrayWithObjects:
                        @"Allan",@"Abbbb",@"Acccc",@"Bccccc",@"Cddddffk",@"Cddkllll",
                @"Ekkflfl",@"Ekljljfg" ,@"Leslie",@"Mm",@"Meinv",@"Meihi",@"Catilin",
                @"Arron", @"211", @"232", @"243", @"264", @"285", @"106", @"311", 
                              @"432", @"543", @"664", @"785", @"806", nil];
    }
    return _dataArr;
}

//如果检索后的数据为空,将原有数据赋值给检索数据
- (NSArray *)arr {
    if (!_arr) {
        self.arr = self.dataArr;
    }
    return _arr;
}
</span>
3.加载UISearchController对象
<font size="4">- (void)viewDidLoad {
    [super viewDidLoad];
    //cell重用机制,调用系统的
    [self.tableView registerClass:[UITableViewCell class]
                                                     forCellReuseIdentifier:@"lock"];
 
    //创建搜索条,将自身设置为展示结果的控制器   
    UISearchController *searchVC =[[UISearchController alloc] 
                                              initWithSearchResultsController:nil];
    //设置渲染颜色
    searchVC.searchBar.tintColor = [UIColor orangeColor];
    //设置状态条颜色  
     searchVC.searchBar.barTintColor = [UIColor orangeColor];
<span style="font-size:18px;">   //设置开始搜索时背景显示与否(很重要) </span>
<span style="font-size:18px;">    searchVC.dimsBackgroundDuringPresentation = NO; </span>
<span style="font-size:18px;">   //适应整个屏幕
   [searchVC.searchBar sizeToFit];
<span style="font-size:18px;">   //设置显示搜索结果的控制器
  <span style="font-size:18px;"> searchVC.searchResultsUpdater = self; </span><span style="font-size:18px;"> //协议(UISearchResultsUpdating)
   //将搜索控制器的搜索条设置为页眉视图
   self.tableView.tableHeaderView = searchVC.searchBar;

 }
</span></span></span></font>

4).实现协议中的方法,必须实现

<span style="font-size:18px;">- (void)updateSearchResultsForSearchController:(UISearchController *)searchController
 {
     //谓词检测
    NSPredicate *predicate = [NSPredicate predicateWithFormat:
                        @"self contains [cd] %@", searchController.searchBar.text];
    //将所有和搜索有关的内容存储到arr数组
    self.arr = [NSMutableArray arrayWithArray:
                             [self.dataArr filteredArrayUsingPredicate:predicate]];
    //重新加载数据     
    [self.tableView reloadData]; 
}
</span>

5).设置UITabelViewController中的其它

<span style="font-size:18px;">#pragma mark - Table view data source
//设置有多少个分区
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    // Return the number of sections.
    
    return 1;
}
//每个分区有多少行数据
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:
                                                                (NSInteger)section {

    return self.arr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
                                                          (NSIndexPath *)indexPath {
     
    self.cell = [tableView dequeueReusableCellWithIdentifier:@"lock" 
                                                            forIndexPath:indexPath];
    
    //设置cell上展示的内容,即搜索后的数据
    self.cell.textLabel.text = self.arr[indexPath.row];
    return self.cell;
}</span>
注.以上是实现搜索框搜索空能.(当搜索内容为空时,返回的时所有数据,如果搜索内容为空,返回空时,需要进行其它修改操作.)





    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多