Welcome to my iphone development code blog

Welcome to my iphone development code blog
"SIMPLICITY IS BEST PROFESSION"

Thursday, July 15, 2010

search functions

here are some useful search functions

- (void) searchbarTextDidBeginEditing:(UISearchBar *)theSearchBar {
searchbar.showsCancelButton = YES;

if(ovController == nil)
ovController = [[OverlayViewController alloc] initWithNibName:@"OverlayView" bundle:[NSBundle mainBundle]];

CGFloat yaxis = self.navigationController.navigationBar.frame.size.height;
CGFloat width = self.view.frame.size.width;
CGFloat height = self.view.frame.size.height;
CGRect frame = CGRectMake(0, yaxis, width, height);
ovController.view.frame = frame;
ovController.view.backgroundColor = [UIColor grayColor];
ovController.view.alpha = 0.5;

ovController.rvController = self;

[self.tableview insertSubview:ovController.view aboveSubview:self.parentViewController.view];

searching = YES;
letUserSelectRow = NO;
self.tableview.scrollEnabled = NO;

}

- (void)searchBar:(UISearchBar *)theSearchBar textDidChange:(NSString *)searchText
{

[copyListofItems removeAllObjects];

if([searchText length] > 0)
{
[ovController.view removeFromSuperview];
searching = YES;
letUserSelectRow = YES;
self.tableview.scrollEnabled = YES;
[self searchTableView];
}
else
{
[self.tableview insertSubview:ovController.view aboveSubview:self.parentViewController.view];
searching = NO;
letUserSelectRow = NO;
self.tableview.scrollEnabled = NO;
}

}
- (void) searchBarSearchButtonClicked:(UISearchBar *)theSearchBar
{
}
- (void) searchBarCancelButtonClicked:(UISearchBar *)theSearchBar
{
[self doneSearching_Clicked:theSearchBar];
}
- (void) searchTableView
{
NSString *searchText = self.searchbar.text;

NSMutableArray *searchArray = [[NSMutableArray alloc] init];
NSEnumerator *userIterator= [usernames objectEnumerator];
NSString *username;

while(username = [userIterator nextObject])
{
[searchArray addObject:username];
}

//NSPredicate *searchpredicate = [NSPredicate predicateWithFormat:@"SELF contains %@",searchText];

//[searchArray filterUsingPredicate:searchpredicate];

for (NSString *sTemp in searchArray)
{
if([[sTemp componentsSeparatedByString:@" "] count ]>1)
{
NSArray *split = [[NSArray alloc] initWithArray:[sTemp componentsSeparatedByString:@" "]];
if ([split count]>0)
{
NSRange titleResultsRange = [[split objectAtIndex:0] rangeOfString:searchText options:(NSCaseInsensitiveSearch|NSAnchoredSearch)];

NSRange titleResultsRange2 = [[split objectAtIndex:1] rangeOfString:searchText options:(NSCaseInsensitiveSearch|NSAnchoredSearch)];
if ((titleResultsRange.length > 0) || (titleResultsRange2.length > 0))
{
[copyListofItems addObject:sTemp];
}
}
}
else
{
NSRange titleResultsRange = [sTemp rangeOfString:searchText options:(NSCaseInsensitiveSearch|NSAnchoredSearch)];
if (titleResultsRange.length > 0)
{
[copyListofItems addObject:sTemp];
}
}

}
[searchArray release];

}

- (void) doneSearching_Clicked:(id)sender
{
self.navigationController.navigationBarHidden = NO;
searchbar.showsCancelButton = NO;
searchbar.text = @"";
[searchbar resignFirstResponder];

letUserSelectRow = YES;
searching = NO;
self.navigationItem.rightBarButtonItem = nil;
self.tableview.scrollEnabled = YES;

[ovController.view removeFromSuperview];
[ovController release];
ovController = nil;
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"Refresh" style:UIBarButtonSystemItemDone target:self action:@selector(loadData)] autorelease];

[self.tableview reloadData];
}

No comments:

Post a Comment