Indexed search in a UIViewController! At any letters inserted into the textfield research, a method is called that executes a database query to obtain tuples as a function of string. If I write a letter after letter, the app crashes because the method is called again while it is still running. Instead, if I write a letter, look forward to the completion of the dispatch queue and write another letter, everything works! But I would like to perform as described in the first case! Here the code:
- (IBAction)searchUser:(id)sender{
dispatch_async(dispatch_get_main_queue(), ^{
[_arrID removeAllObjects];
[_arrNames removeAllObjects];
[_arrPictures removeAllObjects];
[self.tableView reloadData];
});
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
dispatch_async(dispatch_get_main_queue(), ^{
CGRect activityFrame = CGRectMake(self.view.center.x, self.view.center.y, 0.0, 0.0);
_activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:activityFrame];
[[self.view superview] addSubview:_activityIndicator];
_activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
_activityIndicator.color = [UIColor whiteColor];
[_activityIndicator startAnimating];
});
//Here I fill the 3 arrays with details obtained from external queries on database
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
[_activityIndicator stopAnimating];
});
});
}
}
Is there a solution to conduct research in a manner similar to the facebook/google search engine? Given that I don't use UISearchDisplayController, but I just use a textfield that calls the method searchUser every [Editing Did Change] and it filled the Tableview with all results! Please help me!
in your .h file declare the array name
@interface newactivecomposeViewController : UIViewController<
UITextFieldDelegate,UITableViewDataSource,UITableViewDelegate>
{
BOOL SelectionAvailable;
}
@property (strong, nonatomic) NSMutableArray *AllDetails,*SuggestionArray;
@property (strong, nonatomic) UITableView *tablevie;
in your .m file
@synthesize SuggestionArray,AllDetails,tablevie;
- (void)viewDidLoad
{
SuggestionArray=[[NSMutableArray alloc]init]; //for using the searching
self.AllDetails=[[NSMutableArray alloc] init]; //store the all name in this array
tablevie=[[UITableView alloc]initWithFrame:CGRectMake(23, 136, 277, 214)]; //tableview created dynamically
tablevie.dataSource=self;
tablevie.delegate=self;
[self.tablevie registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
[self.view addSubview:tablevie];
NSString *substring = [NSString stringWithString:textField.text];
substring = [substring stringByReplacingCharactersInRange:range withString:string];
[self searchAutocompleteEntriesWithSubstring:substring];
return YES;
}
- (void)searchAutocompleteEntriesWithSubstring:(NSString *)substring {
[SuggestionArray removeAllObjects];
for (NSDictionary *tmp in self.AllDetails) {
NSRange substringRange =[[[tmp objectForKey:@"name"] lowercaseString] rangeOfString:[substring lowercaseString]];
if (substringRange.location==0) {
[SuggestionArray addObject:tmp];
}
}
if ([SuggestionArray count]==0) {
tablevie.hidden=YES;
}
else {
tablevie.hidden=NO;
}
[tablevie reloadData];
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
for (NSString *tmp in getfrinendname) {
NSLog(@"%@", tmp);
if ([[textField.text lowercaseString] isEqualToString:[tmp lowercaseString]]) {
textField.text=tmp;
SelectionAvailable=YES;
break;
}
else {
SelectionAvailable=NO;
}
}
if (!SelectionAvailable) {
if (textField.text.length==0)
{
textField.text=@"";
}
// else if (textField.text.length >0)
// {
//
// [textField resignFirstResponder];
// [tablevie setHidden:YES];
//
// }
else
{
Alert=[[UIAlertView alloc]initWithTitle:@"User not found!" message:@"Please try again" delegate:Nil cancelButtonTitle:@"Ok" otherButtonTitles: nil];
[Alert show];
}
}
#pragma tablevie data source
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog(@"the count==%lu",(unsigned long)[SuggestionArray count]);
return [SuggestionArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;
}
cell.textLabel.text=[[SuggestionArray objectAtIndex:indexPath.row] objectForKey:@"name"]; //load your key or index
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
yourtextfieldname.text=[NSString stringWithFormat:@"%@",[[SuggestionArray objectAtIndex:indexPath.row] objectForKey:@"name"]];
}