UISearchController
是iOS
的一个系统的搜索控件,在平时我们输入信息的时候会出现相应的联想搜索的内容,然后通过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;
- (void)presentSearchController:(UISearchController *)searchController;
(2) UISearchResultsUpdating
该代理中- (void)updateSearchResultsForSearchController:(UISearchController *)searchController
方法每输入一个字符就会执行该方法一次,在此方法中进行数据的更新及表视图的刷新。
3.属性:
@property (nonatomic, assign, getter = isActive) BOOL active;
@property (nonatomic, strong, readonly) UISearchBar *searchBar;
@property (nullable, nonatomic, strong, readonly) UIViewController *searchResultsController;
@property (nonatomic, assign) BOOL dimsBackgroundDuringPresentation
@property (nonatomic, assign) BOOL obscuresBackgroundDuringPresentation NS_AVAILABLE_IOS(9_1);
@property (nonatomic, assign) BOOL hidesNavigationBarDuringPresentation;
4.个性化设置:
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;
@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];
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];
self.mySearchController.obscuresBackgroundDuringPresentation = NO;
self.mySearchController.hidesNavigationBarDuringPresentation = NO;
self.myTableView.tableHeaderView = self.mySearchController.searchBar;
}
- (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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)