2012-12-10 43 views
0

我正在完成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; 


} 


} 

我該如何才能完成這個功能?

回答

0

您可以使用NSURLConnection具有異步執行請求的方法。你只需要給它一個委託或一個完成塊,當請求完成時它會給你打電話。

試試這個例子http://x-techteam.blogspot.in/2011/10/asynchronous-web-service-client-using.html

+0

對不起,我沒有得到你... – Sindhia

+0

U可以使用nsurlconnection方法而不是使用NSXMLParser initWithContentsOfURL方法 – laxonline

+0

試試這個例子http://x-techteam.blogspot.in/2011/10/asynchronous-web-service -client-using.html – laxonline

0

而不是發送同步請求,發送異步之一,並已完成

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]; 
     /* parse as before */ 
     [autocompleteTableView reloadData]; 
    }]; 

你或許應該取消作出新的人之前的當前請求時,它更新表。

+0

嘗試過我們的代碼,但我無法看到autocompletetableView被添加和seen.autocomplete功能不工作..編輯我的代碼可以請你看看,糾正我,如果我錯了..? – Sindhia