UItableView和UISearchBar实现简单表格搜索
UItableView和UISearchBar实现简单表格搜索
/*********************************************************************
文件名称 :SingletonDemoTest.m
网 址 : http://www.hopean.com
创建时间 : 12-11-7
文件描述 :
网 址 : http://www.hopean.com
*********************************************************************/
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UITableView *tmpTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
tmpTableView.delegate = self;
tmpTableView.dataSource = self;
self.tableView = tmpTableView;
[self.view addSubview:tmpTableView];
[tmpTableView release];
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 44)];
self.searchBar.delegate = self;
self.tableView.tableHeaderView = self.searchBar;
NSMutableArray *tmpArr = [[NSMutableArray alloc] initWithObjects:@"aaaaa",@"bbbbb",@"ccccc",@"dddd",@"eeee",@"aaaaa",@"bbbbb",@"ccccc",@"dddd",@"eeee",@"aaaaa",@"bbbbb",@"ccccc",@"dddd",@"eeee",@"aaaaa",@"bbbbb",@"ccccc",@"dddd",@"eeee",@"aaaaa",@"bbbbb",@"ccccc",@"dddd",@"eeee",@"aaaaa",@"bbbbb",@"ccccc",@"dddd",@"eeee",@"aaaaa",@"bbbbb",@"ccccc",@"dddd",@"eeee",@"aaaaa",@"bbbbb",@"ccccc",@"dddd",@"eeee", nil];
self.contentArray = tmpArr;
[tmpArr release];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.contentArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//NSLog(@"RootView:cellForRowAtIndexPath");
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = [self.contentArray objectAtIndex:indexPath.row];
//[contact release]; 不能释放
return cell;
}
#pragma mark - Searching
- (void)updateSearchString:(NSString*)aSearchString
{
//[aSearchString release];
//aSearchString = [[NSString alloc]initWithString:aSearchString];
//[[DataController instance] filterContactsWithLastName:searchString];
NSMutableArray *array = [[NSMutableArray alloc] init];
for(NSString *str in self.contentArray)
{
if ([str rangeOfString:aSearchString options:NSCaseInsensitiveSearch].location != NSNotFound) {
[array addObject:str];
}
}
// if ([array count] == [self.contentArray count]) {
//
// }
self.contentArray = [[NSMutableArray alloc] initWithArray:array];
//array;
[array release];
[self.tableView reloadData];
}
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
[self.searchBar setShowsCancelButton:YES animated:YES];
self.tableView.allowsSelection = NO;
self.tableView.scrollEnabled = NO;
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
[self.searchBar setShowsCancelButton:NO animated:YES];
[self.searchBar resignFirstResponder];
self.tableView.allowsSelection = YES;
self.tableView.scrollEnabled = YES;
self.searchBar.text=@"";
[self updateSearchString:searchBar.text];
self.contentArray = [[NSMutableArray alloc] initWithObjects:@"aaaaa",@"bbbbb",@"ccccc",@"dddd",@"eeee",@"aaaaa",@"bbbbb",@"ccccc",@"dddd",@"eeee",@"aaaaa",@"bbbbb",@"ccccc",@"dddd",@"eeee",@"aaaaa",@"bbbbb",@"ccccc",@"dddd",@"eeee",@"aaaaa",@"bbbbb",@"ccccc",@"dddd",@"eeee",@"aaaaa",@"bbbbb",@"ccccc",@"dddd",@"eeee",@"aaaaa",@"bbbbb",@"ccccc",@"dddd",@"eeee",@"aaaaa",@"bbbbb",@"ccccc",@"dddd",@"eeee", nil];
[self.tableView reloadData];
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
self.tableView.allowsSelection = YES;
self.tableView.scrollEnabled = YES;
[self updateSearchString:self.searchBar.text];
[self.searchBar resignFirstResponder]; //隐藏输入键盘
}