UISearchController控件正好了UISearchBar、UITableView,而且内部提供了良好的封装,可以方便的实现搜索列表。
创建一个工程,并且删除原来的ViewController.h和ViewController.m,重新建立一个文件,命名为ViewController,继承UITableViewController。
AppDelegate.m
//
// AppDelegate.m
// UISearchControllerDemo
//
// Created by Apple on 16/5/26.
// Copyright © 2016年 Apple. All rights reserved.
//
#import "AppDelegate.h"
#import "ViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
ViewController* viewController = [[ViewController alloc] initWithStyle:UITableViewStyleGrouped];
self.window.rootViewController = viewController;
[self.window makeKeyAndVisible];
return YES;
return YES;
}
@end
ViewController.h
//
// ViewController.h
// UISearchControllerDemo_c
//
// Created by apple on 15/7/10.
// Copyright (c) 2015年 fkit.org. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ViewController : UITableViewController <UISearchResultsUpdating,UISearchBarDelegate>
@end
ViewController.m
//
// ViewController.m
// UISearchControllerDemo
//
// Created by Apple on 16/5/26.
// Copyright © 2016年 Apple. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
// ? 必须定义成全局变量
UISearchController *searchController;
// 保存原始表格数据的NSArray对象。
NSArray * tableData;
// 保存搜索结果数据的NSArray对象。
NSArray* searchData;
- (void)viewDidLoad {
NSLog(@"viewDidLoad --> 装载控件");
[super viewDidLoad];
// 初始化原始表格数据
tableData = [NSArray arrayWithObjects:@"Java讲义",
@"轻量级Java EE企业应用实战",
@"Android讲义",
@"Ajax讲义",
@"HTML5/CSS3/JavaScript讲义",
@"iOS讲义",
@"XML讲义",
@"经典Java EE企业应用实战"
@"Java入门与精通",
@"Java基础教程",
@"学习Java",
@"Objective-C基础" ,
@"Ruby入门与精通",
@"iOS开发教程" , nil];
// 注册
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"reuseIdentifier"];
// UISearchController控件整合了UISearchBar,而且内部提供了良好的封装,可以方便的实现搜索列表。
searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
// 注意:必须设置searchBar的大小
searchController.searchBar.frame = CGRectMake(0, 88, 100, 66);
// 设置提示
// searchController.searchBar.prompt = @"查找图书";
searchController.searchBar.placeholder = @"请输入字符";
// 显示CancelButton
searchController.searchBar.showsCancelButton = true;
// 设置这个对象(self)负责更新搜索结果控制器的内容
searchController.searchResultsUpdater = self;
// 搜索结果的背景
// searchController.dimsBackgroundDuringPresentation = NO;
searchController.searchBar.delegate = self;
self.tableView.tableHeaderView = searchController.searchBar;
}
-(void) viewDidAppear:(BOOL)animated{
// self.tableView.frame = [UIScreen mainScreen].applicationFrame;
}
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController{
NSLog(@"updateSearchResultsForSearchController --> ");
// 定义搜索谓词
NSPredicate* pred = [NSPredicate predicateWithFormat:
@"SELF CONTAINS[c] %@" , searchController.searchBar.text];
// 使用谓词过滤tableData数组,返回新数组searchData
searchData = [tableData filteredArrayUsingPredicate:pred];
NSLog(@"searchData -->%lu",[searchData count]);
// 刷新数据(显示新数组)
[self.tableView reloadData];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
NSLog(@"searchBarCancelButtonClicked --> ");
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
if (searchController.active) {
return [searchData count];
}else{
return [tableData count];
}
}
/**/
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseIdentifier" forIndexPath:indexPath];
if (searchController.active) {
cell.textLabel.text = [searchData objectAtIndex:indexPath.row];
}else{
cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
}
return cell;
}
/*
// 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
效果图如下: