UISearchController VS UISearchBar and UISearchDisplayController

来源:互联网 发布:ubuntu u盘系统 编辑:程序博客网 时间:2024/05/01 00:23

一、UISearchBar

这里写图片描述

1、基本属性

  • (1)、@property(nonatomic) UIBarStyle barStyle;

    控件的样式,包括如下枚举:typedef NS_ENUM(NSInteger, UIBarStyle) {    UIBarStyleDefault          = 0,    UIBarStyleBlack            = 1,    UIBarStyleBlackOpaque      = 1, // Deprecated. Use UIBarStyleBlack    UIBarStyleBlackTranslucent = 2, // Deprecated. Use UIBarStyleBlack and set the translucent property to YES};
  • (2)、@property(nonatomic,assign) id delegate;

    设置遵循UISearchBarDelegate代理;
  • (3)、@property(nonatomic,copy) NSString *text;

    设置控件上面的显示的文字;
  • (4)、@property(nonatomic,copy) NSString *prompt;

    设置显示在顶部的单行文字,通常作为一个提示行;
  • (5)、@property(nonatomic,copy) NSString *placeholder;

    半透明的提示文字,输入搜索内容消失,通常作为设置搜素搜文本框的提示文本;
  • (6)、@property(nonatomic) BOOL showsBookmarkButton;

    是否在控件的右端显示一个书的按钮(没有文字的时候);
  • (7)、@property(nonatomic) BOOL showsCancelButton;

    是否显示cancel按钮;
  • (8)、@property(nonatomic) BOOL showsSearchResultsButton NS_AVAILABLE_IOS(3_2);

    是否在控件的右端显示搜索结果按钮(没有文字的时候)
  • (9)、@property(nonatomic, getter=isSearchResultsButtonSelected) BOOL searchResultsButtonSelected NS_AVAILABLE_IOS(3_2);

    搜索结果按钮是否被选中
  • (10)、- (void)setShowsCancelButton:(BOOL)showsCancelButton animated:(BOOL)animated NS_AVAILABLE_IOS(3_0);

    是否动态的显示cancel按钮;
  • (11)、@property(nonatomic,retain) UIColor *tintColor;

    bar的颜色(具有渐变效果);
  • (12)、@property(nonatomic,retain) UIColor *barTintColor NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR;

    设置搜索条的颜色
  • (13)、 @property (nonatomic) UISearchBarStyle searchBarStyle NS_AVAILABLE_IOS(7_0);

    搜索条类型,包含如下枚举值:typedef NS_ENUM(NSUInteger, UISearchBarStyle) {    UISearchBarStyleDefault,    // currently UISearchBarStyleProminent    UISearchBarStyleProminent,  // used my Mail, Messages and Contacts    UISearchBarStyleMinimal     // used by Calendar, Notes and Music} NS_ENUM_AVAILABLE_IOS(7_0);
  • (14)、@property(nonatomic,assign,getter=isTranslucent) BOOL translucent NS_AVAILABLE_IOS(3_0);

    指定控件是否会有透视效果;
  • (15)、@property(nonatomic,copy) NSArray *scopeButtonTitles NS_AVAILABLE_IOS(3_0);

    搜索栏下部的选择栏,数组里面的内容是按钮的标题;
  • (16)、@property(nonatomic) NSInteger selectedScopeButtonIndex NS_AVAILABLE_IOS(3_0);

    搜索栏下部的选择栏按钮的个数;
  • (17)、@property(nonatomic) BOOL showsScopeBar NS_AVAILABLE_IOS(3_0);

    控制搜索栏下部的选择栏是否显示出来;
  • (18)、 @property(nonatomic,retain) UIImage *backgroundImage NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
    背景图片;

2、UISearchBarDelegate协议

  • (1)、编辑代理

    • ①、- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar;

      返回是否称为第一响应者;
    • ②、- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar;

      当搜索文本框开始编辑时激发该方法;
    • ③、- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar;

      返回是否结束第一响应者;
    • ④、- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar;

      当搜索文本框结束编辑时激发该方法;
    • ⑤、- (void)searchBar:(UISearchBar )searchBar textDidChange:(NSString )searchText;

      当搜索文本框内的文本发生改变时激发该方法;
    • ⑥、- (BOOL)searchBar:(UISearchBar )searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString )text NS_AVAILABLE_IOS(3_0);

      当搜索文本框内的文本将要发生改变时激发该方法;
  • (2)、点击按钮

    • ①、- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar;

      当用户单击搜索文本框关联键盘上的”Search”按钮时激发该方法;
    • ②、- (void)searchBarBookmarkButtonClicked:(UISearchBar*)searchBar;

      当用户点击搜索条的书签按钮时激发该方法;
    • ③、- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar;

      当用户单击搜索条的取消按钮时激发该方法;
    • ④、- (void)searchBarResultsListButtonClicked (UISearchBar*)searchBarNS_AVAILABLE_IOS(3_2);

      当用户点击搜索条上的查询结果按钮时激发该方法;
    • ⑤、- (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope NS_AVAILABLE_IOS(3_0);

      当用户点击分段条上的分段按钮时激发该方法;

二、 UISearchDisplayController

1、基本属性

  • (1)、 - (instancetype)initWithSearchBar:(UISearchBar )searchBar contentsController:(UIViewController )viewController;

    初始化    ①、searchBar:用UISearchBar控件初始化;    ②、viewController:和自己关联的UIViewController
  • (2)、 @property(nonatomic,assign) id delegate;

    设置遵循UISearchDisplayDelegate协议的代理;
  • (3)、 @property(nonatomic,getter=isActive) BOOL active;

    设置搜索框是否处在搜索状态;
  • (4)、 - (void)setActive:(BOOL)visible animated:(BOOL)animated;

    是否动态的设置搜索框搜索状态;
  • (5)、 @property(nonatomic,readonly) UISearchBar *searchBar;

    返回UISearchBar控件,为只读属性;
  • (6)、 @property(nonatomic,readonly) UIViewController *searchContentsController;

    返回和自己关联的视图控制器,只读属性;
  • (7)、 @property(nonatomic,readonly) UITableView *searchResultsTableView;

    返回和自己关联的UITableView,只读属性;
  • (8)、@property(nonatomic,assign) id searchResultsDataSource;

    设置遵循UITableViewDataSource协议的代理;
  • (9)、 @property(nonatomic,assign) id searchResultsDelegate;

    设置遵循UITableViewDelegate协议的代理;
  • (10)、 @property (nonatomic, assign) BOOL displaysSearchBarInNavigationBar NS_AVAILABLE_IOS(7_0);

    搜索框是否覆盖导航栏;

2、 UISearchDisplayDelegate

// when we start/end showing the search UI,搜索状态改变

  • (1)、- (void) searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller NS_DEPRECATED_IOS(3_0,8_0);

    将要开始搜索会激发该方法;
  • (2)、- (void) searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller NS_DEPRECATED_IOS(3_0,8_0);

    已经开始搜索会激发该方法;
  • (3)、- (void) searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller NS_DEPRECATED_IOS(3_0,8_0);

    将要结束搜索会激发该方法;
  • (4)、- (void) searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller NS_DEPRECATED_IOS(3_0,8_0);

    已经结束搜索会激发该方法;

// called when the table is created destroyed, shown or hidden. configure as necessary.装载和卸载tableView

  • (5)、- (void)searchDisplayController:(UISearchDisplayController )controller didLoadSearchResultsTableView:(UITableView )tableView NS_DEPRECATED_IOS(3_0,8_0);

    将要加载searchResultsTableView会激发该方法;
  • (6)、- (void)searchDisplayController:(UISearchDisplayController )controller willUnloadSearchResultsTableView:(UITableView )tableView NS_DEPRECATED_IOS(3_0,8_0);

    将要卸载searchResultsTableView会激发该方法;

// called when table is shown/hidden 显示和隐藏tableView

  • (7)、- (void)searchDisplayController:(UISearchDisplayController )controller willShowSearchResultsTableView:(UITableView )tableView NS_DEPRECATED_IOS(3_0,8_0);

    将要显示searchResultsTableView会激发该方法;
  • (8)、- (void)searchDisplayController:(UISearchDisplayController )controller didShowSearchResultsTableView:(UITableView )tableView NS_DEPRECATED_IOS(3_0,8_0);

    已经显示searchResultsTableView会激发该方法;
  • (9)、- (void)searchDisplayController:(UISearchDisplayController )controller willHideSearchResultsTableView:(UITableView )tableView NS_DEPRECATED_IOS(3_0,8_0);

    将要隐藏searchResultsTableView会激发该方法;
  • (10)、- (void)searchDisplayController:(UISearchDisplayController )controller didHideSearchResultsTableView:(UITableView )tableView NS_DEPRECATED_IOS(3_0,8_0);

    已经隐藏searchResultsTableView会激发该方法;

// return YES to reload table. called when search string/option changes. convenience methods on top UISearchBar delegate methods,搜索条件改变时响应

  • (11)- (BOOL)searchDisplayController:(UISearchDisplayController )controller shouldReloadTableForSearchString:(NSString )searchString NS_DEPRECATED_IOS(3_0,8_0);

    搜索框是否可输入或删除;
  • (12)、- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption NS_DEPRECATED_IOS(3_0,8_0);

3、事例

#import "ViewController.h"@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>{    NSMutableArray *data;    NSArray *filterData;    UISearchDisplayController *searchDisplayController;    UITableView* table;}@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    data = [[NSMutableArray alloc]initWithCapacity:100];    for (NSInteger i=0; i<100; i++) {        [data addObject:[NSString stringWithFormat:@"%ld-FlyElephant",(long)i]];    }    UIView* view = [[UIView alloc]init];    [self.view addSubview:view];    table=[[UITableView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStyleGrouped];    table.delegate=self;    table.dataSource=self;    [self.view addSubview:table];    UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width                                                                          , 44)];    searchBar.placeholder = @"搜索";    // 添加 searchbar 到 headerview    table.tableHeaderView = searchBar;    // 用 searchbar 初始化 SearchDisplayController    // 并把 searchDisplayController 和当前 controller 关联起来    searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];    // searchResultsDataSource 就是 UITableViewDataSource    searchDisplayController.searchResultsDataSource = self;    // searchResultsDelegate 就是 UITableViewDelegate    searchDisplayController.searchResultsDelegate = self;    searchDisplayController.searchResultsTitle = @"111";}-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    if (tableView == table) {        return data.count;    }else{        // 谓词搜索        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self contains [cd] %@",searchDisplayController.searchBar.text];        filterData =  [[NSArray alloc] initWithArray:[data filteredArrayUsingPredicate:predicate]];        return filterData.count;    }}-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    static NSString *cellId = @"mycell";    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];    if (cell == nil) {        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];    }    if (tableView == table) {        cell.textLabel.text = data[indexPath.row];    }else{        cell.textLabel.text = filterData[indexPath.row];    }    return cell;}@end

这里写图片描述

三、UISearchController

1、基本属性

  • (1)、- (instancetype)initWithSearchResultsController:(UIViewController *)searchResultsController;

    初始化,searchResultsController可传nil;
  • (2)、 @property (nonatomic, assign) id searchResultsUpdater;

    遵循UISearchResultsUpdating协议的代理;
  • (3)、 @property (nonatomic, assign, getter = isActive) BOOL active;

    返回输入框是否处于输入输入状态;
  • (4)、 @property (nonatomic, assign) id delegate;

    遵循UISearchControllerDelegate协议的代理;
  • (5)、 @property (nonatomic, assign) BOOL dimsBackgroundDuringPresentation;

    是否是要设置背景色为般透明色,设置YES后,点击背景,键盘消失;
  • (6)、 @property (nonatomic, assign) BOOL hidesNavigationBarDuringPresentation;

    返回当搜索框处于输入状态时是否隐藏导航栏;
  • (7)、 @property (nonatomic, retain, readonly) UISearchBar *searchBar;

    返回UISearchBar控件,只读属性;
  • (8)、 - (void)sizeToFit;

    必须要设置一个属性,否则无法显示;

2、UISearchResultsUpdating协议

  • (1)- (void)updateSearchResultsForSearchController:(UISearchController *)searchController;

    当搜索栏文本改变或者称为第一响应者时被调用;

3、事例

#import "ViewController.h"@interface ViewController ()<UISearchResultsUpdating,UISearchControllerDelegate,UITableViewDataSource,UITableViewDelegate>{    UISearchController*_search;}@property(nonatomic,strong)NSArray*dataArray;@property(nonatomic,strong)NSArray*tempArray;@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    self.dataArray=@[@"11",@"22",@"33",@"44"];    UITableView*table=[[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStyleGrouped];    table.delegate=self;    table.dataSource=self;    [self.view addSubview:table];    _search=[[UISearchController alloc]initWithSearchResultsController:nil];   _search.searchResultsUpdater=self;    //是否是要设置背景色为般透明色    _search.dimsBackgroundDuringPresentation=YES;    //必须要设置一个属性,否则无法显示     [_search.searchBar sizeToFit];    NSLog(@"%@",_search.searchBar);    table.tableHeaderView=_search.searchBar;    // Do any additional setup after loading the view, typically from a nib.}-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    return self.dataArray.count;}-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    UITableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:@"ID"];    if (!cell) {        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ID"];    }    cell.textLabel.text=self.dataArray[indexPath.row];    return cell;}#pragma mark 实现协议方法-(void)updateSearchResultsForSearchController:(UISearchController *)searchController{    NSLog(@"开始输入刷新数据");   //这里需要自己来进行相应的处理后刷新数据}@end
0 0