in my project i use search bar it is work fine but it is work 2 or 3 time then don't work !!!!
in interface:
in implementation:
in interface:
Code:
IBOutlet UITableView *libraryTV;
IBOutlet UISearchBar *searchBar; //search bar
NSMutableArray *booktable;
NSMutableArray *sbook;
NSMutableArray *rowoflibrary;
in implementation:
Code:
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
// only show the status bars cancel button while in edit mode
searchBar.showsCancelButton = YES;
searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
// flush the previous search content
[booktable removeAllObjects];
}
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
searchBar.showsCancelButton = NO;
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
[booktable removeAllObjects];// remove all data that belongs to previous search
if([searchText isEqualToString:@""]|| searchText==nil){
[libraryTV reloadData];
return;
}
//NSInteger counter = 0;
for(NSInteger i=0;i<sbook.count;i++)
{
book *bok = (book *)[self.sbook objectAtIndex:i];
NSString *name =bok.book_title;
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
NSRange r = [name rangeOfString:searchText];
if(r.location != NSNotFound)
{
if(r.location== 0)//that is we are checking only the start of the names.
{
[booktable addObject:bok];
}
}
// counter++;
[pool release];
}
[libraryTV reloadData];
}
-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
// if a valid search was entered but the user wanted to cancel, bring back the main list content
[booktable removeAllObjects];
[booktable addObjectsFromArray:sbook];
@try{
[libraryTV reloadData];
}
@catch(NSException *e){
}
[searchBar resignFirstResponder];
searchBar.text = @" ";
}
// called when Search (in our case Done) button pressed
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
[searchBar resignFirstResponder];
}