我正在完成textfield的自動完成文本框功能。我已經完成了它,但是執行時間很慢,這意味着當我按下鍵盤上的任何字符時,首先字母縮放然後顯示實際上,我的自動完成功能列表取自服務url.What需要的是,只要我在鍵盤上鍵入一個字符,我需要它在幾秒鐘內顯示。我知道需要一段時間才能出現因爲它正在服務網址和它的列表中被檢查。但至少如果我能夠比現在更快地獲得它,那麼它就會變成一團糟。textfield的自動完成功能
這是我工作的代碼。
- (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];
[email protected]"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;
}
}
我該如何才能完成這個功能?
對不起,我沒有得到你... – Sindhia
U可以使用nsurlconnection方法而不是使用NSXMLParser initWithContentsOfURL方法 – laxonline
試試這個例子http://x-techteam.blogspot.in/2011/10/asynchronous-web-service -client-using.html – laxonline