#import <UIKit/UIKit.h>

 @interface AppDelegate : UIResponder <UIApplicationDelegate>

 @property (strong, nonatomic) UIWindow *window;

 @end
 #import "AppDelegate.h"
#import "RootViewController.h"
@interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor]; self.window.rootViewController = [[RootViewController alloc] init]; [self.window makeKeyAndVisible];
return YES;
} @end
 #import <UIKit/UIKit.h>

 @interface RootViewController : UIViewController

 @end
 #import "RootViewController.h"
#import "YXPresident.h"
#define Width [UIScreen mainScreen].bounds.size.width
#define Height [UIScreen mainScreen].bounds.size.height
#define gapHeight 20
#define Sheight 50 @interface RootViewController ()<UITableViewDataSource,UITableViewDelegate,UISearchDisplayDelegate,UISearchBarDelegate> @property (nonatomic, strong) UISearchBar *searchBar;
@property (nonatomic, strong) UITableView *mTableView;
@property (nonatomic, strong) NSArray *presidents;
@property (nonatomic, strong) NSArray *filteredPresident; @end @implementation RootViewController - (void)loadView
{
[super loadView];
// 初始化searchBar
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(, gapHeight, Width, Sheight)];
self.searchBar.delegate = self; [self.view addSubview:self.searchBar];
// 初始化mTableView
self.mTableView = [[UITableView alloc] initWithFrame:CGRectMake(, gapHeight+Sheight, Width, Height - Sheight - gapHeight) style:UITableViewStylePlain];
self.mTableView.dataSource = self;
self.mTableView.delegate = self;
[self.view addSubview:self.mTableView];
}
/**
* 初始化数组
*/
- (void)viewDidLoad {
[super viewDidLoad];
self.presidents = [[NSArray alloc] initWithObjects:
[YXPresident presidentWithFirstName:@"George" lastName:@"Washington"],
[YXPresident presidentWithFirstName:@"John" lastName:@"Adams"],
[YXPresident presidentWithFirstName:@"Thomas" lastName:@"Jeffeson"],
[YXPresident presidentWithFirstName:@"James" lastName:@"Madison"],
[YXPresident presidentWithFirstName:@"James" lastName:@"Monroe"],
[YXPresident presidentWithFirstName:@"John Quincy" lastName:@"Adams"],
[YXPresident presidentWithFirstName:@"Andrew" lastName:@"Jackson"],
[YXPresident presidentWithFirstName:@"Martin" lastName:@"van Buren"],
[YXPresident presidentWithFirstName:@"William Henry" lastName:@"Harrison"],
[YXPresident presidentWithFirstName:@"John" lastName:@"Tyler"],
[YXPresident presidentWithFirstName:@"James K" lastName:@"Polk"],
[YXPresident presidentWithFirstName:@"Zachary" lastName:@"Taylor"],
[YXPresident presidentWithFirstName:@"Millard" lastName:@"Fillmore"],
[YXPresident presidentWithFirstName:@"Franklin" lastName:@"Pierce"],
[YXPresident presidentWithFirstName:@"James" lastName:@"Buchanan"],
[YXPresident presidentWithFirstName:@"Abraham" lastName:@"Lincoln"],
[YXPresident presidentWithFirstName:@"Andrew" lastName:@"Johnson"],
[YXPresident presidentWithFirstName:@"Ulysses S" lastName:@"Grant"],
[YXPresident presidentWithFirstName:@"Rutherford B" lastName:@"Hayes"],
[YXPresident presidentWithFirstName:@"James A" lastName:@"Garfield"],
[YXPresident presidentWithFirstName:@"Chester A" lastName:@"Arthur"],
[YXPresident presidentWithFirstName:@"Grover" lastName:@"Cleveland"],
[YXPresident presidentWithFirstName:@"Bejamin" lastName:@"Harrison"],
[YXPresident presidentWithFirstName:@"Grover" lastName:@"Cleveland"],
[YXPresident presidentWithFirstName:@"William" lastName:@"McKinley"],
[YXPresident presidentWithFirstName:@"Theodore" lastName:@"Roosevelt"],
[YXPresident presidentWithFirstName:@"William Howard" lastName:@"Taft"],
[YXPresident presidentWithFirstName:@"Woodrow" lastName:@"Wilson"],
[YXPresident presidentWithFirstName:@"Warren G" lastName:@"Harding"],
[YXPresident presidentWithFirstName:@"Calvin" lastName:@"Coolidge"],
[YXPresident presidentWithFirstName:@"Herbert" lastName:@"Hoover"],
[YXPresident presidentWithFirstName:@"Franklin D" lastName:@"Roosevelt"],
[YXPresident presidentWithFirstName:@"Harry S" lastName:@"Truman"],
[YXPresident presidentWithFirstName:@"Dwight D" lastName:@"Eisenhower"],
[YXPresident presidentWithFirstName:@"John F" lastName:@"Kennedy"],
[YXPresident presidentWithFirstName:@"Lyndon B" lastName:@"Johnson"],
[YXPresident presidentWithFirstName:@"Richard" lastName:@"Nixon"],
[YXPresident presidentWithFirstName:@"Gerald" lastName:@"Ford"],
[YXPresident presidentWithFirstName:@"Jimmy" lastName:@"Carter"],
[YXPresident presidentWithFirstName:@"Ronald" lastName:@"Reagan"],
[YXPresident presidentWithFirstName:@"George H W" lastName:@"Bush"],
[YXPresident presidentWithFirstName:@"Bill" lastName:@"Clinton"],
[YXPresident presidentWithFirstName:@"George W" lastName:@"Bush"],
[YXPresident presidentWithFirstName:@"Barack" lastName:@"Obama"],
nil]; } #pragma mark - UITableViewDelegate -
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (self.filteredPresident != nil) {
return [self.filteredPresident count];
}
else{
return [self.presidents count];
}
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *Identify = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Identify];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Identify];
}
YXPresident *president = [[YXPresident alloc] init];
if (self.filteredPresident != nil) {
president = [self.filteredPresident objectAtIndex:indexPath.row];
}else{
president = [self.presidents objectAtIndex:indexPath.row];
}
if (president) {
cell.textLabel.text = [NSString stringWithFormat:@"%@ %@",president.firstName, president.lastName];
}
return cell;
} #pragma mark - Content Filtering
- (void)filterContentForSearchText:(NSString *)searchText
{
// 设置搜索条件
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"firstName CONTAINS[cd] %@ OR lastName CONTAINS[cd] %@",searchText,searchText];
// 返回搜索结果
self.filteredPresident = [self.presidents filteredArrayUsingPredicate:predicate];
}
#pragma mark - UISearchBarDelegate -
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
[self filterContentForSearchText:searchText ];
if (self.filteredPresident.count == && [searchBar.text isEqual: @""]) {
self.filteredPresident = nil;
}
[self.mTableView reloadData];
} @end
 #import <Foundation/Foundation.h>

 @interface YXPresident : NSObject

 @property (nonatomic, copy) NSString *firstName;
@property (nonatomic, copy) NSString *lastName; + (YXPresident *)presidentWithFirstName:(NSString *)firstName lastName:(NSString *)lastName; @end
 #import "YXPresident.h"

 @implementation YXPresident
@synthesize firstName, lastName; + (YXPresident *)presidentWithFirstName:(NSString *)firstName lastName:(NSString *)lastName
{
YXPresident *president = [[YXPresident alloc] init];
president.firstName = firstName;
president.lastName = lastName;
return president;
} @end

UITableView + UISearchBar 实现搜索功能的更多相关文章

  1. ios UISearchDisplayController 实现 UITableView 搜索功能

    UISearchDisplayController 是苹果专为 UITableView 搜索封装的一个类. 里面内置了一个 UITableView 用于显示搜索的结果.它可以和一个需要搜索功能的 co ...

  2. iOS--- UITableView + UISearchDisplayController - - - - -实现搜索功能

    iOS中UISearchDisplayController用于搜索,搜索栏的重要性我们就不说了,狼厂就是靠搜索起家的,现在越来越像一匹没有节操的狼,UC浏览器搜索栏现在默认自家的神马搜索,现在不管是社 ...

  3. iOS8 UISearchViewController搜索功能讲解

    在iOS8以前我们实现搜索功能需要用到UISearchbar和UISearchDisplayController, 在iOS8之后呢, UISearchController配合UITableView的 ...

  4. iOS8 UISearchViewController搜索功能讲解 分类: ios技术 2015-07-14 10:23 76人阅读 评论(0) 收藏

    在iOS8以前我们实现搜索功能需要用到UISearchbar和UISearchDisplayController, 在iOS8之后呢, UISearchController配合UITableView的 ...

  5. iOS中利用UISearchBar实现搜索

    先把源码贴出来 https://github.com/losedMemory/ZSSearchBar   这是我在github上写的一个Demo,大家可以看看 在大多数app中都会用到搜索功能,那么搜 ...

  6. Android搜索功能的案例,本地保存搜索历史记录......

    开发的APP有一个搜索功能,并且需要显示搜索的历史记录,我闲暇之余帮她开发了这个功能,现把该页面抽取成一个demo分享给大家. 实现效果如图所示:  本案例实现起来很简单,所以可以直接拿来嵌入项目中使 ...

  7. Yii 1开发日记 -- 搜索功能及Checkbox的实现

    用yii 1实现后台的搜索功能,效果如下图: 1.模型中: public function search() { $criteria = new CDbCriteria; //独立高级搜索 if(is ...

  8. SharePoint 2013 搜索功能,列表项目不能完全被索引

    描述 最近一个站点,需要开启搜索功能,然后创建内容源,开始爬网,发现列表里只有一部分被索引,很多项目没有被索引,甚是奇怪,如下图(其实列表里有80几条项目). 首先爬网账号是系统账号.服务器管理员,所 ...

  9. idea 光标变成粗体且当前文件搜索功能无法使用的问题

    今天安装了idea最新版,安装完成后发现光标变成了粗体,并且快捷键在使用时出现了问题,比如:ctrl+F搜索功能无法使用 经过反复修改配置也无法改善情况,后来一次重启看到下面小窗弹出有关vim的一个提 ...

随机推荐

  1. CSS3:渐变大全

    渐变大全 声明 最后的老写法镜像渐变可能不太准确.其余都完全正确 <!DOCTYPE html> <html> <head> <meta http-equiv ...

  2. jQuery EasyUI DataGrid Checkbox 数据设定与取值

    纯粹做个记录,以免日后忘记该怎么设定. 这一篇将会说明两种使用 jQuery EasyUI DataGrid 的 Checkbox 设定方式,以及在既有数据下将 checked 为 true 的该笔数 ...

  3. POJ 3041 匈牙利算法模板题

    一开始预习是百度的算法 然后学习了一下 然后找到了学长的ppt 又学习了一下.. 发现..居然不一样... 找了模板题试了试..百度的不好用 反正就是wa了..果然还是应当跟着学长混.. 图两边的点分 ...

  4. mybatis sql in 查询(mybatis sql语句传入参数是list)mybatis中使用in查询时in怎么接收值

    1.in查询条件是list时 <select id="getMultiMomentsCommentsCounts" resultType="int"> ...

  5. Worker 工作 后台js 工作

    <script type="text/javascript"> var w; function startWorker() { if (typeof (Worker) ...

  6. node socket onmessage

    <script src="//cdn.sockjs.org/sockjs-0.3.min.js"></script> <script> var ...

  7. Why Stored Procedures?

    http://www.w3resource.com/mysql/mysql-procedure.php Stored procedures are fast. MySQL server takes s ...

  8. General protection fault Exceptions in Linux/IA32 Systems

    Computer Systems A Programmer's Perspective Second Edition Exception number Description Exception cl ...

  9. cursor:pointer

    .cursor_hand{ cursor:pointer; }

  10. 在Delphi中如何动态创建dbf数据库(一)?

    table2.Close; table2.Active:=false; table2.Exclusive:=true; table2.TableName:='h:\gzkd\sds'; table2. ...