UITableView + UISearchBar 实现搜索功能
#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 实现搜索功能的更多相关文章
- ios UISearchDisplayController 实现 UITableView 搜索功能
UISearchDisplayController 是苹果专为 UITableView 搜索封装的一个类. 里面内置了一个 UITableView 用于显示搜索的结果.它可以和一个需要搜索功能的 co ...
- iOS--- UITableView + UISearchDisplayController - - - - -实现搜索功能
iOS中UISearchDisplayController用于搜索,搜索栏的重要性我们就不说了,狼厂就是靠搜索起家的,现在越来越像一匹没有节操的狼,UC浏览器搜索栏现在默认自家的神马搜索,现在不管是社 ...
- iOS8 UISearchViewController搜索功能讲解
在iOS8以前我们实现搜索功能需要用到UISearchbar和UISearchDisplayController, 在iOS8之后呢, UISearchController配合UITableView的 ...
- iOS8 UISearchViewController搜索功能讲解 分类: ios技术 2015-07-14 10:23 76人阅读 评论(0) 收藏
在iOS8以前我们实现搜索功能需要用到UISearchbar和UISearchDisplayController, 在iOS8之后呢, UISearchController配合UITableView的 ...
- iOS中利用UISearchBar实现搜索
先把源码贴出来 https://github.com/losedMemory/ZSSearchBar 这是我在github上写的一个Demo,大家可以看看 在大多数app中都会用到搜索功能,那么搜 ...
- Android搜索功能的案例,本地保存搜索历史记录......
开发的APP有一个搜索功能,并且需要显示搜索的历史记录,我闲暇之余帮她开发了这个功能,现把该页面抽取成一个demo分享给大家. 实现效果如图所示: 本案例实现起来很简单,所以可以直接拿来嵌入项目中使 ...
- Yii 1开发日记 -- 搜索功能及Checkbox的实现
用yii 1实现后台的搜索功能,效果如下图: 1.模型中: public function search() { $criteria = new CDbCriteria; //独立高级搜索 if(is ...
- SharePoint 2013 搜索功能,列表项目不能完全被索引
描述 最近一个站点,需要开启搜索功能,然后创建内容源,开始爬网,发现列表里只有一部分被索引,很多项目没有被索引,甚是奇怪,如下图(其实列表里有80几条项目). 首先爬网账号是系统账号.服务器管理员,所 ...
- idea 光标变成粗体且当前文件搜索功能无法使用的问题
今天安装了idea最新版,安装完成后发现光标变成了粗体,并且快捷键在使用时出现了问题,比如:ctrl+F搜索功能无法使用 经过反复修改配置也无法改善情况,后来一次重启看到下面小窗弹出有关vim的一个提 ...
随机推荐
- T-SQL DBMS
dbo 默认架构schema 从一个数据库操作另个数据库的表的时候,要select * from 数据库.dbo.biao 表名前面的dbo是一个默认架构schema,一个架构还有 ...
- Mininet实验 使用l2_multi模块寻找最短路径实验
参考:使用l2_multi模块寻找最短路径实验 1. 实验目的 1.认识VND并且掌握其基本使用方法. 2.学会使用pox控制器的l2_multi模块寻找主机间的最短传输路径. 2. 实验原理 VND ...
- Nginx 笔记与总结(9)rewrite 重写规则
重写的规则可以放在 serverer 里,也可以放在 location 里. rewrite 规则: 常用的命令有 ① if(条件){} 设定条件,再进行重写 if 语法: if 空格 (条件){ ...
- Ubuntu 12.04 卸载 VMware
cd /usr/bin sudo vmware-installer -u vmware-player
- JAVA函数的返回值类型详解以及生成随机数的例题
函数的四要素:函数名.输入.输出(返回).加工. 函数分为两种:一种是有返回值得函数,一种是没有返回值的函数. 1. 定义:没有返回值的函数:(当我不需要函数的计算结果再拿出来进行运算的时候,我就不需 ...
- js 和 jquery 获取页面和滚动条的高度 视口高度文档高度
js 和 jquery 获取页面和滚动条的高度 //页面位置及窗口大小 function GetPageSize() { var scrW, scrH; if(window.innerHeight & ...
- Bluetooth HFP介绍
目录 1. 介绍 1.1 目的 1.2 使用场景 1.3 依赖关系 1.4 协议栈 1.5 角色 2. 应用层 3. 空白章节 4. 互操作性要求 4.1 介绍 4.2 Service Level C ...
- C语言文法定义及C程序的推导过程
program à external_declaration | program external_declaration <程序> -> <外部声明> | < ...
- discuz函数解析--写日志
public static function writelog($file, $log) { global $_G; $yearmonth = dgmdate(TIMESTAMP, 'Ym', $_G ...
- 【Android测试】【随笔】与 “美丽说” 测试同事交流
◆版权声明:本文出自胖喵~的博客,转载必须注明出处. 转载请注明出处:http://www.cnblogs.com/by-dream/p/5405432.html 分享者简介 雪晗,3年+测试经验,现 ...