前言
為了方便童鞋們更深刻的了解UISearchController的使用,寫了這樣一篇文章扫茅,來看看吧~~
概述
iOS8.0之前,搜索欄基本都是靠UISearchBar+UISearchDisplayController這對(duì)好基友來實(shí)現(xiàn)的。iOS8.0之后朵耕,蘋果成全了它們,UISearchController便由此誕生淋叶。UISearchController的出現(xiàn)宣告著UISearchDisplayController的遺棄阎曹。
UISearchController不同于UISearchDisplayController的最大之處在于UISearchController繼承自UIViewController,是一個(gè)實(shí)實(shí)在在的視圖控制器煞檩,而UISearchDisplayController繼承自NSObject,說白了是一個(gè)工具類处嫌。
常用屬性及方法一覽
UISearchController 常用屬性
// 搜索界面的狀態(tài),只讀屬性斟湃。
@property (nonatomic, assign, getter = isActive) BOOL active;
// 決定在搜索時(shí)熏迹,底層的內(nèi)容是否要變暗。
@property (nonatomic, assign) BOOL dimsBackgroundDuringPresentation;
// 搜索欄使用的時(shí)候是否需要隱藏NavigationBar凝赛,默認(rèn)值為true注暗。
@property (nonatomic, assign) BOOL hidesNavigationBarDuringPresentation;
// 自定義的搜索結(jié)果Controller
@property (nullable, nonatomic, strong, readonly) UIViewController *searchResultsController;
// 搜索欄墓猎,只讀屬性捆昏。
@property (nonatomic, strong, readonly) UISearchBar *searchBar;
// UISearchController的代理
@property (nullable, nonatomic, weak) id <UISearchControllerDelegate> delegate;
// UISearchResultsUpdating的代理
@property (nullable, nonatomic, weak) id <UISearchResultsUpdating> searchResultsUpdater;
UISearchBar 常用屬性
// 控件的樣式
@property(nonatomic) UIBarStyle barStyle;
// UISearchBarDelegate的代理
@property(nullable,nonatomic,weak) id<UISearchBarDelegate> delegate;
// 控件上的顯示文字
@property(nullable,nonatomic,copy) NSString *text;
// 控件上方的提示文字
@property(nullable,nonatomic,copy) NSString *prompt;
// 控件的占位提示文字
@property(nullable,nonatomic,copy) NSString *placeholder;
// 控件是否透視效果
@property(nonatomic,assign,getter=isTranslucent) BOOL translucent
UISearchControllerDelegate 方法
方法由上到下的執(zhí)行順序
- (void)presentSearchController:(UISearchController *)searchController;
- (void)willPresentSearchController:(UISearchController *)searchController;
- (void)didPresentSearchController:(UISearchController *)searchController;
- (void)willDismissSearchController:(UISearchController *)searchController;
- (void)didDismissSearchController:(UISearchController *)searchController;
UISearchResultsUpdating 方法
// 更新搜索結(jié)果
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController;
UISearchBarDelegate 常用方法
// 開始編輯
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar;
// 結(jié)束編輯
- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar;
// 正在編輯
- (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
與tableView的簡單關(guān)聯(lián)使用
上代碼,先添加代理
@interface
<
UITableViewDelegate,
UITableViewDataSource,
UISearchResultsUpdating,
UISearchControllerDelegate,
UISearchBarDelegate
>
@end
將要跨方法使用的東東寫成屬性
@property (nonatomic, strong) UISearchController *searchController;
@property (nonatomic, strong) UITableView *tableView;
// 存數(shù)據(jù)的數(shù)組
@property (nonatomic, strong) NSMutableArray *dataArray;
// 搜索到的結(jié)果數(shù)組
@property (nonatomic, strong) NSMutableArray *searchArray;
具體方法實(shí)現(xiàn)
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self createSearch];
[self createTableView];
[self createArray];
}
- (void)createTableView {
self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
[self.view addSubview:_tableView];
_tableView.tableHeaderView = _searchController.searchBar;
[_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:reusableIdentifier];
}
- (void)createArray {
// 創(chuàng)建數(shù)據(jù)數(shù)組
self.dataArray = [NSMutableArray array];
// 給數(shù)據(jù)數(shù)組賦初值
for (int i = 100; i <= 1000; i++) {
[_dataArray addObject:[NSString stringWithFormat:@"%d",i]];
}
// 為了全面測(cè)試,數(shù)據(jù)數(shù)組增加英文與中文
[_dataArray addObject:@"Small Tiger"];
[_dataArray addObject:@"冠軍"];
// 創(chuàng)建結(jié)果數(shù)組
self.searchArray = [NSMutableArray array];
}
- (void)createSearchController {
self.searchController = [[UISearchController alloc]initWithSearchResultsController:nil];
_searchController.searchResultsUpdater = self;
_searchController.searchBar.frame = CGRectMake(_searchController.searchBar.frame.origin.x, _searchController.searchBar.frame.origin.y, _searchController.searchBar.frame.size.width, 44.0);
_searchController.dimsBackgroundDuringPresentation = NO;
}
UITableView協(xié)議方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (_searchController.active) {
return _searchArray.count;
} else {
return _dataArray.count;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reusableIdentifier];
if (_searchController.active) {
cell.textLabel.text = _searchArray[indexPath.row];
} else {
cell.textLabel.text = _dataArray[indexPath.row];
}
return cell;
}
UISearchResultsUpdating協(xié)議方法
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
NSString *searchString = self.searchController.searchBar.text;
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[c] %@",searchString];
// 移除原數(shù)據(jù)
if (_searchArray != nil) {
[self.searchArray removeAllObjects];
}
// 過濾數(shù)據(jù)
self.searchArray = [NSMutableArray arrayWithArray:[_dataArray filteredArrayUsingPredicate:predicate]];
// 刷新表格
[_tableView reloadData];
}
這樣就簡單實(shí)現(xiàn)了UISearchController與UITableView的關(guān)聯(lián)使用
NSPredicate謂詞
光有搜索框沒什么卵用,還要有搜索功能毙沾,內(nèi)部的功能就需要謂詞來實(shí)現(xiàn)了
謂詞的功能很強(qiáng)大骗卜,同時(shí)它還可以使用正則表達(dá)式,可以實(shí)現(xiàn)各種郵箱驗(yàn)證,手機(jī)號(hào)驗(yàn)證寇仓,以及各種查找功能勇皇。
基本常用的謂詞:
- 邏輯運(yùn)算符 && AND || OR 都可以用
- IN包含
- 模糊查詢
- 以……開頭 BEGINSWITH
- 以……結(jié)尾 ENDSWITH
- 包含….字符 CONTAINS
- 用like進(jìn)行模糊查詢
通配符:*表示任意個(gè)字符 ?表示單個(gè)字符
like *a 以a結(jié)尾
like a* 以a開頭
like *a* 包含a字符
like ?a* 第二個(gè)字符為a的字符串
創(chuàng)建Book類
Book.h
@interface Book : NSObject
{
NSInteger _price;
NSString* _bookName;
}
- (instancetype)initWithPrice:(NSInteger)price andBookName:(NSString *)bookName;
@end
Book.m
#import "Book.h"
@implementation Book
- (instancetype)initWithPrice:(NSInteger)price andBookName:(NSString *)bookName {
if (self = [super init]) {
_price = price;
_bookName = bookName;
}
return self;
}
- (NSString *)description {
return [NSString stringWithFormat:@"Book price:%li,named %@",_price,_bookName];
}
@end
main.m
int main(int argc, const char * argv[]) {
@autoreleasepool {
Book* book1 = [[Book alloc] initWithPrice:20 andBookName:@"C Programming"];
Book* book2 = [[Book alloc] initWithPrice:32 andBookName:@"C++ Programming"];
Book* book3 = [[Book alloc] initWithPrice:18 andBookName:@"Java Programming"];
Book* book4 = [[Book alloc] initWithPrice:45 andBookName:@"OC guiding"];
Book* book5 = [[Book alloc] initWithPrice:28 andBookName:@"iOS guiding"];
NSArray* books = [NSArray arrayWithObjects:book1,book2,book3,book4,book5, nil];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"price > %i",30];
NSArray *filterArray = [books filteredArrayUsingPredicate:predicate];
NSLog(@"%@",filterArray);
// 邏輯運(yùn)算符 和 IN
predicate = [NSPredicate predicateWithFormat:@"bookName IN {'C Programming','C++ Programming'} AND price > 30"];
filterArray = [books filteredArrayUsingPredicate:predicate];
NSLog(@"%@",filterArray);
// 模糊查詢 和 用通配符查詢
predicate = [NSPredicate predicateWithFormat:@"bookName CONTAINS 'guiding' || bookName like '*Program*' "]; //包含guiding或者包含Program
filterArray = [books filteredArrayUsingPredicate:predicate];
NSLog(@"%@",filterArray);
}
return 0;
}
心靈雞湯
一青年途經(jīng)某地焚刺,碰到一位老者敛摘,問:“這里如何?”老者反問:“你家鄉(xiāng)如何乳愉?”青年答:“糟透了兄淫!我很討厭÷Γ”老者說:“這里也一樣糟捕虽。”后來又來了個(gè)青年問同樣問題坡脐,老者也同樣反問泄私,青年回答說:“我家鄉(xiāng)很好,我很想念备闲∩味耍”老者便說:“這里也一樣好√裆埃”旁聽者覺得詫異咧纠,問老人家為何前后說法不一致呢?老者說:“你管得著嗎泻骤?”
難受的時(shí)候摸摸自己的胸漆羔,告訴自己是個(gè)漢子,要堅(jiān)強(qiáng)~