代码改变世界

UITableView + UISearchBar 实现搜索功能

2015-09-14 14:00  甘雨路  阅读(219)  评论(0编辑  收藏  举报
1 #import <UIKit/UIKit.h>
2 
3 @interface AppDelegate : UIResponder <UIApplicationDelegate>
4 
5 @property (strong, nonatomic) UIWindow *window;
6 
7 
8 @end
 1 #import "AppDelegate.h"
 2 #import "RootViewController.h"
 3 @interface AppDelegate ()
 4 
 5 @end
 6 
 7 @implementation AppDelegate
 8 
 9 
10 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
11     self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
12     // Override point for customization after application launch.
13     self.window.backgroundColor = [UIColor whiteColor];
14     
15     self.window.rootViewController = [[RootViewController alloc] init];
16     
17     [self.window makeKeyAndVisible];
18     return YES;
19 }
20 
21 @end
1 #import <UIKit/UIKit.h>
2 
3 @interface RootViewController : UIViewController
4 
5 @end
  1 #import "RootViewController.h"
  2 #import "YXPresident.h"
  3 #define Width [UIScreen mainScreen].bounds.size.width
  4 #define Height [UIScreen mainScreen].bounds.size.height
  5 #define gapHeight 20
  6 #define Sheight 50
  7 
  8 @interface RootViewController ()<UITableViewDataSource,UITableViewDelegate,UISearchDisplayDelegate,UISearchBarDelegate>
  9 
 10 @property (nonatomic, strong) UISearchBar *searchBar;
 11 @property (nonatomic, strong) UITableView *mTableView;
 12 @property (nonatomic, strong) NSArray *presidents;
 13 @property (nonatomic, strong) NSArray *filteredPresident;
 14 
 15 @end
 16 
 17 @implementation RootViewController
 18 
 19 - (void)loadView
 20 {
 21     [super loadView];
 22     // 初始化searchBar
 23     self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, gapHeight, Width, Sheight)];
 24     self.searchBar.delegate = self;
 25     
 26     [self.view addSubview:self.searchBar];
 27     // 初始化mTableView
 28     self.mTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, gapHeight+Sheight, Width, Height - Sheight - gapHeight) style:UITableViewStylePlain];
 29     self.mTableView.dataSource = self;
 30     self.mTableView.delegate = self;
 31     [self.view addSubview:self.mTableView];
 32 }
 33 /**
 34  * 初始化数组
 35  */
 36 - (void)viewDidLoad {
 37     [super viewDidLoad];
 38     self.presidents = [[NSArray alloc] initWithObjects:
 39                        [YXPresident presidentWithFirstName:@"George" lastName:@"Washington"],
 40                        [YXPresident presidentWithFirstName:@"John" lastName:@"Adams"],
 41                        [YXPresident presidentWithFirstName:@"Thomas" lastName:@"Jeffeson"],
 42                        [YXPresident presidentWithFirstName:@"James" lastName:@"Madison"],
 43                        [YXPresident presidentWithFirstName:@"James" lastName:@"Monroe"],
 44                        [YXPresident presidentWithFirstName:@"John Quincy" lastName:@"Adams"],
 45                        [YXPresident presidentWithFirstName:@"Andrew" lastName:@"Jackson"],
 46                        [YXPresident presidentWithFirstName:@"Martin" lastName:@"van Buren"],
 47                        [YXPresident presidentWithFirstName:@"William Henry" lastName:@"Harrison"],
 48                        [YXPresident presidentWithFirstName:@"John" lastName:@"Tyler"],
 49                        [YXPresident presidentWithFirstName:@"James K" lastName:@"Polk"],
 50                        [YXPresident presidentWithFirstName:@"Zachary" lastName:@"Taylor"],
 51                        [YXPresident presidentWithFirstName:@"Millard" lastName:@"Fillmore"],
 52                        [YXPresident presidentWithFirstName:@"Franklin" lastName:@"Pierce"],
 53                        [YXPresident presidentWithFirstName:@"James" lastName:@"Buchanan"],
 54                        [YXPresident presidentWithFirstName:@"Abraham" lastName:@"Lincoln"],
 55                        [YXPresident presidentWithFirstName:@"Andrew" lastName:@"Johnson"],
 56                        [YXPresident presidentWithFirstName:@"Ulysses S" lastName:@"Grant"],
 57                        [YXPresident presidentWithFirstName:@"Rutherford B" lastName:@"Hayes"],
 58                        [YXPresident presidentWithFirstName:@"James A" lastName:@"Garfield"],
 59                        [YXPresident presidentWithFirstName:@"Chester A" lastName:@"Arthur"],
 60                        [YXPresident presidentWithFirstName:@"Grover" lastName:@"Cleveland"],
 61                        [YXPresident presidentWithFirstName:@"Bejamin" lastName:@"Harrison"],
 62                        [YXPresident presidentWithFirstName:@"Grover" lastName:@"Cleveland"],
 63                        [YXPresident presidentWithFirstName:@"William" lastName:@"McKinley"],
 64                        [YXPresident presidentWithFirstName:@"Theodore" lastName:@"Roosevelt"],
 65                        [YXPresident presidentWithFirstName:@"William Howard" lastName:@"Taft"],
 66                        [YXPresident presidentWithFirstName:@"Woodrow" lastName:@"Wilson"],
 67                        [YXPresident presidentWithFirstName:@"Warren G" lastName:@"Harding"],
 68                        [YXPresident presidentWithFirstName:@"Calvin" lastName:@"Coolidge"],
 69                        [YXPresident presidentWithFirstName:@"Herbert" lastName:@"Hoover"],
 70                        [YXPresident presidentWithFirstName:@"Franklin D" lastName:@"Roosevelt"],
 71                        [YXPresident presidentWithFirstName:@"Harry S" lastName:@"Truman"],
 72                        [YXPresident presidentWithFirstName:@"Dwight D" lastName:@"Eisenhower"],
 73                        [YXPresident presidentWithFirstName:@"John F" lastName:@"Kennedy"],
 74                        [YXPresident presidentWithFirstName:@"Lyndon B" lastName:@"Johnson"],
 75                        [YXPresident presidentWithFirstName:@"Richard" lastName:@"Nixon"],
 76                        [YXPresident presidentWithFirstName:@"Gerald" lastName:@"Ford"],
 77                        [YXPresident presidentWithFirstName:@"Jimmy" lastName:@"Carter"],
 78                        [YXPresident presidentWithFirstName:@"Ronald" lastName:@"Reagan"],
 79                        [YXPresident presidentWithFirstName:@"George H W" lastName:@"Bush"],
 80                        [YXPresident presidentWithFirstName:@"Bill" lastName:@"Clinton"],
 81                        [YXPresident presidentWithFirstName:@"George W" lastName:@"Bush"],
 82                        [YXPresident presidentWithFirstName:@"Barack" lastName:@"Obama"],
 83                        nil];
 84 
 85 }
 86 
 87 #pragma mark - UITableViewDelegate - 
 88 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
 89 {
 90     if (self.filteredPresident != nil) {
 91         return [self.filteredPresident count];
 92     }
 93     else{
 94         return [self.presidents count];
 95     }
 96 }
 97 
 98 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 99 {
100     static NSString *Identify = @"cell";
101     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Identify];
102     if (cell == nil) {
103         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Identify];
104     }
105     YXPresident *president = [[YXPresident alloc] init];
106     if (self.filteredPresident != nil) {
107         president = [self.filteredPresident objectAtIndex:indexPath.row];
108     }else{
109         president = [self.presidents objectAtIndex:indexPath.row];
110     }
111     if (president) {
112         cell.textLabel.text = [NSString stringWithFormat:@"%@ %@",president.firstName, president.lastName];
113     }
114     return cell;
115 }
116 
117 #pragma mark - Content Filtering
118 - (void)filterContentForSearchText:(NSString *)searchText
119 {
120     // 设置搜索条件
121     NSPredicate *predicate = [NSPredicate predicateWithFormat:@"firstName CONTAINS[cd] %@ OR lastName CONTAINS[cd] %@",searchText,searchText];
122     // 返回搜索结果
123     self.filteredPresident = [self.presidents filteredArrayUsingPredicate:predicate];
124 }
125 #pragma mark - UISearchBarDelegate - 
126 - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
127 {
128     [self filterContentForSearchText:searchText ];
129     if (self.filteredPresident.count == 0 && [searchBar.text  isEqual: @""]) {
130         self.filteredPresident = nil;
131     }
132     [self.mTableView reloadData];
133 }
134 
135 @end
 1 #import <Foundation/Foundation.h>
 2 
 3 @interface YXPresident : NSObject
 4 
 5 @property (nonatomic, copy) NSString *firstName;
 6 @property (nonatomic, copy) NSString *lastName;
 7 
 8 + (YXPresident *)presidentWithFirstName:(NSString *)firstName lastName:(NSString *)lastName;
 9 
10 @end
 1 #import "YXPresident.h"
 2 
 3 @implementation YXPresident
 4 @synthesize firstName, lastName;
 5 
 6 + (YXPresident *)presidentWithFirstName:(NSString *)firstName lastName:(NSString *)lastName
 7 {
 8     YXPresident *president = [[YXPresident alloc] init];
 9     president.firstName = firstName;
10     president.lastName = lastName;
11     return president;
12 }
13 
14 @end