#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. 对.net系统架构改造的一点经验和教训(转)

    在互联网行业,基于Unix/Linux的网站系统架构毫无疑问是当今主流的架构解决方案,这不仅仅是因为Linux本身足够的开放性,更因为围绕传统Unix/Linux社区有大量的成熟开源解决方案,覆盖了网 ...

  2. Ubuntu 安装 ImageMagic(6.9.1-6)及 PHP 的 imagick (3.0.1)扩展

    关于 ImageMagic 和 imagick 的介绍,见<图片处理神器ImageMagick以及PHP的imagick扩展> 和 <Ubuntu下安装ImageMagick和Mag ...

  3. MySQL 数据库设计 笔记与总结(4)维护优化

    [维护和优化的工作] ① 维护数据字典 ② 维护索引 ③ 维护表结构 ④ 在适当的时候对表进行水平拆分或垂直拆分 [维护数据字典] a 使用第三方工具对数据字典进行维护 b 利用数据库本身的备注字段来 ...

  4. jQuery+CSS 简单代码实现遮罩层( 兼容主流浏览器 )

    /* ** jQuery版本:jQuery-1.8.3.min.js ** 浏览器:Chrome( v31.0.1650.63 m ),IE11,Firefox( v32.0.1 ),IETester ...

  5. userDefaults

    // // RootViewController.m #import "RootViewController.h" @interface RootViewController () ...

  6. windows下Gulp安装

    目录: 1.安装nodejs2.使用命令行3.npm介绍4.选装cnpm5.全局安装gulp6.新建package.json文件7.本地安装gulp插件8.新建gulpfile.js文件9.运行gul ...

  7. PHP不仅仅是PHP

    PHP不仅仅是PHP   PHP不仅仅是PHP. PHP的面试不仅仅会问到PHP语言本身(基本都是基础和细节),下面列举的主要是都是高级工程师的要求 比如:PHP中include和require的区别 ...

  8. SqlServer 高版本数据库 降级

    SQL Server2012 1 首先就来介绍一下SQL Server2012数据库的降级方法,成功之后突然感觉很简单,但是没成功之前很郁闷.废话少说,直接开始. 打开SQL Server2012,连 ...

  9. (转)JAVA 调用matlab

    本文仅用于学习. 原文地址链接:http://blog.csdn.net/wannshan/article/details/5907877 前段时间摸索了java调用matlab东西,不说学的有多深, ...

  10. ajax普通弹窗;Bootstrp弹窗

    1.普通弹窗 主页面: <head> <meta http-equiv="Content-Type" content="text/html; chars ...