本文主要是介绍iOS UISearchController TableView 实现简单搜索功能,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
搜索功能大家基本都能会用到, 有很多方法可以实现搜索功能,
下面是用UISearchController设置为tableView表头实现简单的搜索功能,简单易懂.
1.创建继承于UITableViewController的Controller, 遵循<UISearchResultsUpdating, UISearchControllerDelegate>协议
UISearchResultsUpdating协议 用来基于用户输入搜索框的信息来更新搜索结果的
- updateSearchResultsForSearchController:
Required
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
这篇关于iOS UISearchController TableView 实现简单搜索功能的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!