iOS UISearchController TableView 實現簡單搜尋功能
搜尋功能大家基本都能會用到, 有很多方法可以實現搜尋功能,
下面是用UISearchController設定為tableView表頭實現簡單的搜尋功能,簡單易懂.
1.建立繼承於UITableViewController的Controller, 遵循<UISearchResultsUpdating, UISearchControllerDelegate>協議
UISearchResultsUpdating協議 用來基於使用者輸入搜尋框的資訊來更新搜尋結果的
UISearchControllerDelegate協議 UISearchController的代理方法(4個) 比較無腦, 見名知意
- (void)willPresentSearchController:(UISearchController *)searchController;
- (void)didPresentSearchController:(UISearchController *)searchController;
- (void)willDismissSearchController:(UISearchController *)searchController;
- (void)didDismissSearchController:(UISearchController *)searchController;
不多說了上程式碼 註釋挺全的 可直接使用
一..
.h檔案
#import <UIKit/UIKit.h>
@interface BaseViewController : UITableViewController <UISearchResultsUpdating, UISearchControllerDelegate>
// 空的搜尋字串展示所有的資料
// 如果有字串 再用過濾器展示有關資訊
@property (nonatomic, copy) NSString *filterString;
@property (copy) NSArray *allResults;
// 搜尋結果集
@property (readwrite, copy) NSArray *visibleResults;
// 建立搜尋框
@property (nonatomic, strong) UISearchController *searchController;
@end
二...
.m檔案
#import "BaseViewController.h"
@interface BaseViewController ()
@end
@implementation BaseViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 建立搜尋控制器
self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
// 設定搜尋控制器 更新的內容
// 搜尋框輸入時 更新列表
self.searchController.searchResultsUpdater = self;
// 設定為NO的時候 列表的單元格可以點選 預設為YES無法點選無效
self.searchController.dimsBackgroundDuringPresentation = NO;
// 設定代理
self.searchController.delegate = self;
// 保證搜尋導航欄中可見
[self.searchController.searchBar sizeToFit];
// 把搜尋框 設定為表頭
self.tableView.tableHeaderView = self.searchController.searchBar;
self.definesPresentationContext = YES;
// 給結果資料集
self.allResults = @[@"Here's", @"to", @"the", @"crazy", @"ones.", @"The", @"misfits.", @"The", @"rebels.", @"The", @"troublemakers.", @"The", @"round", @"pegs", @"in", @"the", @"square", @"holes.", @"The", @"ones", @"who", @"see", @"things", @"differently.",
@"They're", @"not", @"fond", @"of", @"rules.", @"And", @"they", @"have", @"no", @"respect", @"for", @"the", @"status", @"quo.", @"You", @"can", @"quote", @"them,", @"disagree", @"with", @"them,", @"glorify", @"or", @"vilify", @"them.", @"About", @"the", @"only",
@"thing", @"you", @"can't", @"do", @"is", @"ignore", @"them.", @"Because", @"they", @"change", @"things.", @"They", @"push", @"the", @"human", @"race", @"forward.", @"And", @"while", @"some", @"may", @"see", @"them", @"as", @"the", @"crazy", @"ones,", @"we",
@"see", @"genius.", @"Because", @"the", @"people", @"who", @"are", @"crazy", @"enough", @"to", @"think", @"they", @"can", @"change", @"the", @"world,", @"are", @"the", @"ones", @"who", @"do."];
// 預設讓結果陣列等於所有的陣列資料
self.visibleResults = self.allResults;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark --- 重寫 filterString的set方法
- (void)setFilterString:(NSString *)filterString
{
_filterString = filterString;
// 如果搜尋字串為空 設定結果陣列
if (!filterString || filterString.length <= 0) {
self.visibleResults = self.allResults;
} else {
NSPredicate *filterPredicate = [NSPredicate predicateWithFormat:@"self contains[c] %@", filterString];
self.visibleResults = [self.allResults filteredArrayUsingPredicate:filterPredicate];
}
[self.tableView reloadData];
}
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.visibleResults.count;
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.textLabel.text = self.visibleResults[indexPath.row];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *identifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"%@", self.visibleResults[indexPath.row]);
}
#pragma mark ---- UISearchResultsUpdating
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
if (!searchController.active) {
return;
}
self.filterString = searchController.searchBar.text;
}
#pragma mark --- 設定searchController 代理方法
// 將要返回
- (void)willDismissSearchController:(UISearchController *)searchController
{
// 點選cancel的時候 陣列還原 重新整理表
self.visibleResults = self.allResults;
[self.tableView reloadData];
}
-(void)didDismissSearchController:(UISearchController *)searchController
{
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the specified item to be editable.
return YES;
}
*/
/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
} else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
*/
/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/
/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*/
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end