searchbar自动匹配搜索内容

-(viod) viewDidLoad{///////////////////////////////////////////////////////////////////////////////////////////////// Add searchbarmySearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.bounds.size.width, 40)];mySearchBar.placeholder=@"Please Enter";mySearchBar.delegate = self;mySearchBar.autocorrectionType = UITextAutocorrectionTypeNo;mySearchBar.autocapitalizationType = UITextAutocapitalizationTypeNone;[self.view addSubview:mySearchBar];[mySearchBar release];//////////////////////////////////////////////////////////////////////////////////////////////// Add search result table viewsearchTableView=[[UITableView alloc] initWithFrame:CGRectMake(0.0, 40.0, self.view.bounds.size.width, 160) style:UITableViewStylePlain];searchTableView.rowHeight=30;searchTableView.dataSource=self;searchTableView.delegate=self;}#pragma mark searchBar functions- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{searchArray=[[NSMutableArray alloc] init];NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES); NSString *path=[paths objectAtIndex:0]; NSString *filename=[path stringByAppendingPathComponent:@"Info.plist"];//this file name which the info is savedNSMutableArray *array=[[NSMutableArray alloc] initWithContentsOfFile:filename];NSInteger length=[searchText length];for (int i=0; i<[array count]; i++) {NSString* text=[array objectAtIndex:i];NSString* textTemp=[text substringToIndex:length];if ([textTemp isEqualToString:searchText]) {[searchArray addObject:text];}}[searchTableView reloadData];}- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {[mySearchBar setShowsCancelButton:YES animated:YES];[self.view addSubview:searchTableView];//add TableView }- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {mySearchBar.text=@"";[[self.view.subviews objectAtIndex:[self.view.subviews count]-1] removeFromSuperview];//remove the tableView if clicking the cancel button[mySearchBar setShowsCancelButton:NO animated:YES];[mySearchBar resignFirstResponder];searchArray=[[NSMutableArray alloc] init];[searchTableView reloadData];}- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {[self searchBarPin];NSString* addressToData=[NSString stringWithFormat:@"%@",[[[resultArray objectForKey:@"results"] objectAtIndex:0] objectForKey:@"formatted_address"]];//把新输入的记录准备放入文件里[self writeIntoDataForAddress:[addressToData retain]];}-(void) writeIntoDataForAddress:(NSString*)address{NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES); NSString *path=[paths objectAtIndex:0]; NSString *filename=[path stringByAppendingPathComponent:@"Info.plist"];NSInteger function=0;NSMutableArray *array=[[NSMutableArray alloc] initWithContentsOfFile:filename];NSMutableArray *array2=[[NSMutableArray alloc]init]; //我试过想在提取的array里面直接addObject,然后写入文件,但没有成功,所以我重新写一个array,所以第一步就是要把原来的记录全部放入新的array里面,//所以有第一个循环for (int i=0; i<[array count]; i++) {[array2 addObject:[array objectAtIndex:i]];}//下面这个循环比较新输入的记录在文件有没有这条记录for (int i=0; i<[array2 count]; i++) {if ([address isEqualToString:[array2 objectAtIndex:i]]) {function=1;//表示文件里面有这行数据,,如果是0表示没有这行数据,需要添加}}if (function==0) {[array2addObject:address];}//NSLog(@"%d",[array2 count]);[array2 writeToFile:filenameatomically:YES]; [array2 release];}#pragma mark tableView delegate- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {return [searchArray count];}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {static NSString *CellIdentifier = @"Cell";UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];if (cell == nil) {cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier: CellIdentifier] autorelease];}// Configure the cell…NSUInteger row = [indexPath row];cell.textLabel.text = [searchArray objectAtIndex:row];cell.textLabel.font = [UIFont systemFontOfSize:15.0];return cell;}- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {mySearchBar.text=[[searchArray objectAtIndex:[indexPath row]] retain];[self searchBarPin];}-(void) searchBarPin{//这里写当click search buton的时候需要干什么的coding}最后别忘了要建Info.plist这个文件来保存记录。不好意思,因为我不在mac上,只能贴代码了。至于interface,你就按照提示的错误,在interface上添加吧。

在时间里面我们什么也不能留下,包括痛苦,快乐和生命。

searchbar自动匹配搜索内容

相关文章:

你感兴趣的文章:

标签云: