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
上一篇:PowerDesigner 缺省值 引号 问题


下一篇:洛谷——P2388 阶乘之乘