iOS --- 搜索框UISearchController的使用(iOS8.0以后替代UISearchBar + UISearchDisplayController的组合)

在iOS 8.0以上版本中, 我们可以使用UISearchController来非常方便地在UITableView中添加搜索框. 而在之前版本中, 我们还是必须使用UISearchBar + UISearchDisplayController的组合方式.

添加UISearchController属性:

@property(strong, nonatomic) UISearchController *searchController;
@property(strong, nonatomic) NSMutableArray *allCities; // 所有城市
@property(strong, nonatomic) NSMutableArray *filteredCities; // 根据searchController搜索的城市

UISearchController初始化

在viewDidLoad中初始化UISearchController:

  self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
  self.searchController.searchResultsUpdater = self;
  self.searchController.dimsBackgroundDuringPresentation = false;
  [self.searchController.searchBar sizeToFit];
  self.searchController.searchBar.backgroundColor = UIColorFromHex(0xdcdcdc);
  self.tableView.tableHeaderView = self.searchController.searchBar;

UISearchResultsUpdating协议

使用UISearchController要继承UISearchResultsUpdating协议, 实现其中的UISearchResultsUpdating方法.

#pragma mark - searchController delegate

- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
  [self.filteredCities removeAllObjects];
  NSPredicate *searchPredicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[c] %@", self.searchController.searchBar.text];
  self.filteredCities = [[self.allCities filteredArrayUsingPredicate:searchPredicate] mutableCopy];
  dispatch_async(dispatch_get_main_queue(), ^{
      [self.tableView reloadData];
  });
}

UISearchController的searchBar中的内容一旦发生变化, 就会调用该方法. 在其中, 我们可以使用NSPredicate来设置搜索过滤的条件.

更新UITableView

引入UISearchController之后, UITableView的内容也要做相应地变动: 即cell中要呈现的内容是allCities, 还是filteredCities.

这一点, 可以通过UISearchController的active属性来判断, 即判断输入框是否处于active状态.

UITableView相关的很多方法都要根据active来做判断:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  if (!self.searchController.active) {
    return self.cityKeys.count;
  } else {
    return 1;
  }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  if (!self.searchController.active) {
    NSString *key = self.cityKeys[section];
    NSArray *citySection = self.cityDict[key];
    return citySection.count;
  } else {
    return self.filteredCities.count;
  }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  static NSString *CellIdentifier = @"Cell";

  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
  }
  // 根据UISearchController的active属性来判断cell中的内容
  if (!self.searchController.active) {
    NSString *key = self.cityKeys[indexPath.section];
    cell.textLabel.text = [self.cityDict[key] objectAtIndex:indexPath.row];
  } else {
    cell.textLabel.text = self.filteredCities[indexPath.row];
  }
  return cell;
}

UISearchController的移除

在viewWillDisappear中要将UISearchController移除, 否则切换到下一个View中, 搜索框仍然会有短暂的存在.

- (void)viewWillDisappear:(BOOL)animated {
  [super viewWillDisappear:animated];
  if (self.searchController.active) {
    self.searchController.active = NO;
    [self.searchController.searchBar removeFromSuperview];
  }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-12-24 02:13:18

iOS --- 搜索框UISearchController的使用(iOS8.0以后替代UISearchBar + UISearchDisplayController的组合)的相关文章

搜索框UISearchController的使用(iOS8.0以后替代UISearchBar + UISearchDisplayController)

1.searchResultsUpdater:设置显示搜索结果的控制器 ? 1     _mySearchController.searchResultsUpdater = self; 2.dimsBackgroundDuringPresentation:设置开始搜索时背景显示与否 ? 1     _mySearchController.dimsBackgroundDuringPresentation = NO; 3.[searchBar sizeToFit]:设置searchBar位置自适应

iOS 搜索框之UISearchBar

一.初始化:UISearchBar继承于UIView,我们可以像创建View那样创建searchBar UISearchBar * bar = [[UISearchBar alloc]initWithFrame:CGRectMake(20, 100, 250, 40)]; [self.view addSubview:bar]; 二.属性 @property(nonatomic)        UIBarStyle              barStyle; 这个属性可以设置searchBar的

iOS 搜索框控件 最简单的dome

刚学习搜索框控件,写了个最简单的dome #import <UIKit/UIKit.h> .h @interface ViewController : UIViewController<UISearchBarDelegate,UISearchDisplayDelegate,UITableViewDataSource,UITableViewDelegate> @property (nonatomic,strong) UISearchDisplayController *searchD

iOS搜索框UISearchBar

当你在seachBar中输入字母之前的时候,只是用鼠标选中searchBar的时候,如图 终端输出截图如下:(这个时候调用先shouldBeginEditing,之后调用didBeginEditing,) 当你希望选中UISearchBar的时候,键盘自动调用加载到界面,你需要将下面函数的返回值设置为YES: - (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar { NSLog(@"shouldBeginEditing"

设置“搜索”框

** UISearchController是iOS 8** 之后推出的一个新的控件, 用于创建搜索条, 及管理搜索事件, 使用这个, 我们可以很容易的创建属于自己的搜索框, 下面就来看看这个控件的一些使用. 一. 基本使用( 同一个控制器 ) ** UISearchController一般是和UITableView结合使用, 很少会单独使用他, 而且使用UITableView** 来展示数据, 也是最佳的选择. 他的API十分简单: // 初始化方法, 参数是展示搜索结果的控制器, 如果是在当前

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

效果如下: ViewController.h 1 #import <UIKit/UIKit.h> 2 #import "DropDownListViewController.h" 3 4 @interface ViewController : UITableViewController<UISearchBarDelegate, PassValueDelegate> 5 @property (strong, nonatomic) UISearchBar *sear

iOS - Swift UISearchController仿微信搜索框

0x01.创建一个UISearchController 如果传入的searchController为nil,则表示搜索的结果在当前控制器中显示,现在我让它在searchVC中显示. // 创建searchResultVC let searchVC = UIViewController() // 设置背景颜色为红色 searchVC.view.backgroundColor = UIColor.red let searchController = UISearchController(search

iOS UISearchController 搜索框

#import <Foundation/Foundation.h> @interface Student : NSObject @property(strong,nonatomic) NSString *name; @property(strong,nonatomic) NSString *pic; @property(strong,nonatomic) NSString *tel; -(Student *)initWithDic:(NSDictionary *)dic; +(Student

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

1.UISearchBar(效果如下:) ①创建UISearchBar对象 //初始化,定义frame UISearchBar *bar = [[UISearchBar alloc] initWithFrame:CGRectMake (0, 50, self.view.frame.size.width, 80)]; //添加到控制器的视图上 [self.view addSubview:bar]; ②UISerachBar的属性 //autocapitalizationType:包含4种类型,但是