#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

最新文章

  1. shell-脚本入门【转】
  2. PHP 遍历数组的方法汇总
  3. 【转】javascript 中that的含义示例介绍
  4. python 归档tarfile,zipfile学习
  5. android 处理网络状态——无网,2g,3g,wifi,ethernet,other
  6. 对C++进行优化了的android-ndk-r6-crystax-2
  7. 为你的网页中添加一些空格&amp;nbsp;
  8. 从设计模式说起JAVA I/O流
  9. js 放置 cookie、获取 cookie、删除 cookie
  10. RHM-M60型挖掘机力矩限制器/载荷指示器
  11. SPOJ GSS1_Can you answer these queries I(线段树区间合并)
  12. WebService之CXF注解之三(Service接口实现类)
  13. Exp3 免杀原理与实践 20164303 景圣
  14. BigDecimal比较2个值是否相等,不能用equals,而要用compareTo
  15. redis-cluster集群搭建
  16. POJ 3090 Visible Lattice Points 【欧拉函数】
  17. 洛谷 P1057 传球游戏 【dp】(经典)
  18. jdk与eclipse位数不一致出现的问题
  19. 移动端页面滑动时候警告:Unable to preventDefault inside passive event listener due to target being treated as passive.
  20. 20172333 2017-2018-2 《Java程序设计》第4周学习总结

热门文章

  1. LR中日志设置和日志函数
  2. PHP MongoDB 扩展安装配置
  3. spark mllib 之线性回归
  4. JavaScript中的prototype
  5. OC接收数据时毫秒转date时间最简略方法
  6. sqlserver 简单的事物用法
  7. centos常用命令
  8. 针对JD-GUI
  9. Ubuntu-1404 GDB 调试C++报错
  10. ASP.NET页面间数据传递的方法&lt;转&gt;