0

テキストフィールドのオートコンプリートテキストフィールド機能を実行していますが、パフォーマンス時間が遅いため、キーパッドの任意の文字を押すと、最初に文字がズームされ、しばらくするとテキストフィールドに表示されます。オートコンプリート機能のリストはサービスのURLから取得されます。必要なのは、キーパッドで文字を入力するとすぐに、数秒以内に表示される必要があることです。サービスでチェックインされているため、表示されるまでに時間がかかることがわかりました。 urlとそのリスト。しかし、少なくとも私が今よりもいくらか速くそれを得ることができれば、それは勇気があるでしょう。

これは私が取り組んでいるコードです。

- (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;


}


}

この機能を交互に実行するにはどうすればよいですか?

4

2 に答える 2

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];
   }];

新しいリクエストを作成する前に、現在のリクエストをキャンセルする必要があります。

于 2012-12-10T10:58:48.430 に答える
0

NSURLConnectionリクエストを非同期で実行するためのhasメソッドを使用できます。デリゲートまたは完了ブロックを指定するだけで、リクエストが完了すると呼び出されます。

この例を試してくださいhttp://x-techteam.blogspot.in/2011/10/asynchronous-web-service-client-using.html

于 2012-12-10T10:33:47.447 に答える