【iOS】—— 浅谈UISearchController

UISearchControlleriOS的一个系统的搜索控件,在平时我们输入信息的时候会出现相应的联想搜索的内容,然后通过UITableView展示到搜索框的下面,供我们选择。原本还想用UITextField来实现这个功能,人家现在有,那就浅学习一手。

一、需要遵守的协议:

因为它用到的UITableView所以一定是要用到UITableViewDelegate, UITableViewDataSource的,另外我们要实现其联想搜索功能还的遵守这两个协议UISearchControllerDelegate, UISearchResultsUpdating

二、属性及方法:

1.初始化方法:

- (instancetype)initWithSearchResultsController:(nullable UIViewController *)searchResultsController;参数为nil,表示使用当前控制器作为展示结果的控制器。否则,使用指定的控制器作为显示结果的控制器。

2.两个代理:

(1) UISearchControllerDelegate该代理中的方法用来告知用户结果视图的状态(即将出现,已经出现,即将消失,已经消失)。

//当自动呈现或删除发生时,调用这些方法。如果自己呈现或取消搜索控制器,它们将不会被调用。
- (void)willPresentSearchController:(UISearchController *)searchController;
- (void)didPresentSearchController:(UISearchController *)searchController;
- (void)willDismissSearchController:(UISearchController *)searchController;
- (void)didDismissSearchController:(UISearchController *)searchController;

// 在搜索控制器的搜索栏同意开始编辑或“active”被设置为YES时调用。如果您选择不呈现控制器或不实现此方法,则将代表您执行默认表示。
- (void)presentSearchController:(UISearchController *)searchController;

(2) UISearchResultsUpdating该代理中- (void)updateSearchResultsForSearchController:(UISearchController *)searchController方法每输入一个字符就会执行该方法一次,在此方法中进行数据的更新及表视图的刷新。

3.属性:

// active 属性就是当前 searchController 是不是处于激活状态.只要点击 searchBar,active 就会被置为 true.
@property (nonatomic, assign, getter = isActive) BOOL active;

// 搜索框,根据需求可进行自定义
@property (nonatomic, strong, readonly) UISearchBar *searchBar;

// 只读属性,展示结果的视图控制器
@property (nullable, nonatomic, strong, readonly) UIViewController *searchResultsController;

// 搜索时,背景是否变暗色 ,默认为YES
@property (nonatomic, assign) BOOL dimsBackgroundDuringPresentation
  
// 搜索时,背景是否变模糊 默认为YES
@property (nonatomic, assign) BOOL obscuresBackgroundDuringPresentation NS_AVAILABLE_IOS(9_1);

// 是否隐藏导航栏,默认为YES
@property (nonatomic, assign) BOOL hidesNavigationBarDuringPresentation; 

4.个性化设置:

// 通过此种方式可以获取到searchBar内部的输入框,根据需要可对其进行个性化设置
UITextField *searchTextField = [self.systemSearchController.searchBar valueForKey:@"_searchField"];

三、实例演示:

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UITableViewDelegate, UITableViewDataSource, UISearchControllerDelegate, UISearchResultsUpdating>

@property (nonatomic, strong) UISearchController *mySearchController;  //搜索框
@property (nonatomic, strong) UITableView *myTableView;  //展示的tableview
@property (nonatomic, strong) NSMutableArray *mySearchResultArray;  //搜索到的数据
@property (nonatomic, strong) NSMutableArray *myDataArray;  //总数据

@end

ViewController.m

#import "ViewController.h"

#define myWidth [UIScreen mainScreen].bounds.size.width
#define myHeight [UIScreen mainScreen].bounds.size.height

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //初始化数据源
    self.myDataArray = [[NSMutableArray alloc] initWithObjects:@"11", @"22", @"33", @"214324", @"464564", nil];
    self.mySearchResultArray = [[NSMutableArray alloc] init];
    
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, myWidth, myHeight) style:UITableViewStylePlain];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self.view addSubview:self.myTableView];
    
    self.mySearchController = [[UISearchController alloc] initWithSearchResultsController:nil];
    self.mySearchController.delegate = self;
    // 设置搜索代理
    self.mySearchController.searchResultsUpdater = self;
    // 设置搜索条大小
    [self.mySearchController.searchBar sizeToFit];
    // 设置搜索期间背景视图是否取消操作,default is YES
    self.mySearchController.obscuresBackgroundDuringPresentation = NO;
    // 设置搜索期间是否隐藏导航条,default is YES
    self.mySearchController.hidesNavigationBarDuringPresentation = NO;
    // 将 searchBar 添加到表格的开头
    self.myTableView.tableHeaderView = self.mySearchController.searchBar;
}

/*
只要搜索框的文字发生了改变,这个方法就会触发。searchController.searchBar.text 为搜索框内输入的内容
*/
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {

    // 清除上一次的搜索结果
    [self.mySearchResultArray removeAllObjects];

    // 将搜索的结果存放到数组中
    for (NSString *str in self.myDataArray) {
        //查找搜索内容在该字符串的位置
        NSRange range = [str rangeOfString:searchController.searchBar.text];
        if (range.length) {
            [self.mySearchResultArray addObject:str];
        }
    }

    // 重新加载表格视图,不加载的话将不会显示搜索结果
    [self.myTableView reloadData];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [[UITableViewCell alloc] init];
    cell.textLabel.text = self.mySearchResultArray[indexPath.row];
    return cell;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.mySearchResultArray.count;
}


@end

2412142
4234234

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值