UISearchBar
#import <UIKit/UIKit.h>
@interface SearchViewController :UIViewController<UISearchBarDelegate,UISearchDisplayDelegate,UITableViewDelegate,UITableViewDataSource>
{
}
@property(nonatomic,strong)UISearchBar* searchBar;
@property(nonatomic, strong)UITableView* tableView;
@property(nonatomic , strong)NSMutableArray* array1;
@property(nonatomic , strong)NSMutableArray* array2;
@end
#import "SearchViewController.h"
@interface SearchViewController ()
@end
@implementation SearchViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
_searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0,320, 40)];
self.searchBar.placeholder = @"请输入所要查询的关键字";
self.searchBar.showsCancelButton = YES;
[self.view addSubview:_searchBar];
UISearchDisplayController* searchDisPlay = [[UISearchDisplayController alloc]initWithSearchBar:self.searchBar contentsController:self];
searchDisPlay.delegate = self;
searchDisPlay.searchResultsDataSource = self;
searchDisPlay.searchResultsDelegate = self;
_array1 =[[NSMutableArray alloc] initWithCapacity:2];
[self.array1 addObject:@"1"];
[self.array1 addObject:@"12"];
[self.array1 addObject:@"123"];
[self.array1 addObject:@"1234"];
[self.array1 addObject:@"12345"];
[self.array1 addObject:@"54321"];
[self.array1 addObject:@"5432"];
[self.array1 addObject:@"543"];
[self.array1 addObject:@"54"];
[self.array1 addObject:@"5"];
_array2 = [[NSMutableArray alloc] initWithCapacity:2];
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 40, 320,self.view.bounds.size.height - 40)];
self.tableView.delegate = self;
self.tableView.dataSource = self;
[self.view addSubview:self.tableView];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(tableView == self.searchDisplayController.searchResultsTableView)
{
return self.array2.count;
}
return self.array1.count;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString* identifier = @"cell";
UITableViewCell* cell = [tableViewdequeueReusableCellWithIdentifier:identifier];
if(!cell)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:identifier] autorelease];
}
if(tableView == self.searchDisplayController.searchResultsTableView)
{
cell.textLabel.text = [self.array2 objectAtIndex:indexPath.row];
}
else
{
cell.textLabel.text = [self.array1 objectAtIndex:indexPath.row];
}
return cell;
}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption
{
return YES;
}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
[self.array2 removeAllObjects];
for(int i = 0; i < self.array1.count; i++)
{
if([[self.array1 objectAtIndex:i] hasPrefix:searchString])
{
[self.array2 addObject:[self.array1 objectAtIndex:i]];
}
}
return YES;
}