I m acccomplishing autocomplete textfield feature for a textfield.I have done it but the performance time is slow,which means when I press any character on the keypad then first the letter is zooming and then it is being displayed in textfield after sometime .Actually my list for autocomplete feature is taken from a service url.What is need is as soon as I type a character on keypad I need it to be displayed within a fraction of seconds.I knew it takes sometime to appear as it is being checked in service url and its list.But atleast if I could gain it somewhat faster than now then it would be gud.
This is the code I m working on.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if(textField==CompanyName)
{
autocompleteTableView.hidden = NO;
NSString *substring = [NSString stringWithString:textField.text];
substring = [substring stringByReplacingCharactersInRange:range withString:string];
[self searchAutocompleteEntriesWithSubstring:substring];
return YES;
if([CompanyName.text length]==0)
{
autocompleteTableView.hidden = YES;
}
}
}
- (void)searchAutocompleteEntriesWithSubstring:(NSString *)substring {
NSString *urlString= [NSString stringWithFormat:@"http://221.190.132.116/services/AutoCompService.svc/GetEmpNames/?p=%@",substring];
NSURL *jsonUrl =[NSURL URLWithString:urlString];
NSURLRequest *request = [NSMutableURLRequest requestWithURL:jsonUrl];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
parser = [[NSXMLParser alloc] initWithData:data];
currentHtmlElement=@"5";
[parser setDelegate:self];
[parser setShouldProcessNamespaces:NO];
[parser setShouldReportNamespacePrefixes:NO];
[parser setShouldResolveExternalEntities:NO];
[parser parse];
[parser release];
if([arr4 count]!=0)
{
self.autocompleteUrls = [[NSMutableArray alloc] init];
if(autocompleteTableView)
[autocompleteTableView removeFromSuperview];
autocompleteTableView = [[UITableView alloc] initWithFrame:CGRectMake(10, 110, 195, [arr4 count]*30) style:UITableViewStyleGrouped];
autocompleteTableView.delegate = self;
autocompleteTableView.dataSource = self;
autocompleteTableView.scrollEnabled = YES;
autocompleteTableView.backgroundColor = [UIColor lightTextColor];
autocompleteTableView.rowHeight=28;
autocompleteTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[self.view addSubview:autocompleteTableView];
[autocompleteUrls removeAllObjects];
for(int i=0;i<[arr4 count];i++)
{
NSString *curString = [[arr4 objectAtIndex:i] valueForKey:@"Name"];
[autocompleteUrls addObject:curString];
}
}
[autocompleteTableView reloadData];
}];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
CompanyName.text = selectedCell.textLabel.text;
autocompleteTableView.hidden=YES;
}
}
how can I accomplish this feature alternativtly?
you can use
NSURLConnectionhas methods for performing the request asynchronously. You just give it a delegate or a completion block and it will call you when the request is complete.Try this example http://x-techteam.blogspot.in/2011/10/asynchronous-web-service-client-using.html