2

カスタム値からテキストフィールドをオートコンプリートしたい

ここでGoogleとSOを調査し​​たUITextFieldオートコンプリート-iPhoneSDK

私はこれを試しました:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

    if ([string isEqualToString:@"Fac"]) {
        _clientField.text = @"Factory";
    }

    return YES;
}

問題は、予測値が入力されず、入力さFactoryれた値だけが入力されることです。Fac

編集

答えに基づいてこれを試しました...

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

if ([string isEqualToString:@"Fac"]) {
    _clientField.text = [_clientField.text stringByReplacingCharactersInRange:range
                                                   withString:@"Factory"];
  }

  return NO;
}

まだ同じ

4

4 に答える 4

7

HTAutocompleteTextFieldがその仕事をするように聞こえます:

https://github.com/hoteltonight/HTAutocompleteTextField

基本的には、オートコンプリートデータソースとして値の配列を指定するだけで、ユーザーが入力するとテキストフィールドに提案が表示されます。

于 2013-01-25T20:34:40.670 に答える
2

You should have tried to NSLog string it always returns the last character the user inputs (except if you copy/paste a string: in this case your code works).

Actually, doing what you're trying to do is not as straightforward as it seems to be. Here what I came up with:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSLog(@"string: %@", string);

    NSString *currentText = textField.text;
    NSString *nextText = [currentText stringByAppendingString:string];

    if ([nextText isEqualToString:@"Fac"])
    {
        textField.text = @"Factory";
        return NO;
    }
    return YES;
}  

If you don't build the nextText yourself then the autocompletion will be delayed. If you only check the value of textField.text for instance, and that you type "Fac" the completion will occur on the next character input.
When you autocomplete the field you have to return NO also so the string is not added at the end of the text field (F -> Fa -> Factoryc, you don't want that). So you say that textField should not change characters in range... And YOU change the textField value yourself.

Hope this will help you to understand what's going on in this UITextFieldDelegate method.

于 2013-01-17T21:58:10.100 に答える
2

I checked your sample code and the text field's delegate is not set.

You can set it in your view controller using

_clientField.delegate = self;

Additionally you need to use a slightly different method to get the text which the users sees. Something like this:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSString *currentString = [textField.text stringByReplacingCharactersInRange:range withString:string];
    if ([currentString isEqualToString:@"Fac"]) {
        textField.text = @"Factory";
        return NO;
    }
    return YES;
}

Note that you probably want to fine tune this a bit, since it e.g. also autocompletes when the user deletes text. But this should get you on the right track.

于 2013-01-18T10:35:26.850 に答える
1

You can also have a look at this control (NHAutoCompleteTextField). In the example source code in the given link, you will see the search list filters as the user types.

NHAutoCompleteTextField is easy to integrate:

#import "NHMainHeader.h"

autoCompleteTextField = [[NHAutoCompleteTextField alloc] initWithFrame:CGRectMake(x, y, width, 18)];  // 18 - example height
[autoCompleteTextField setDropDownDirection:NHDropDownDirectionDown];
[autoCompleteTextField setDataSourceDelegate:self];
[autoCompleteTextField setDataFilterDelegate:self];

and flexible to do customization as per your need. for example, create cell as per you need here:

- (UITableViewCell *)autoCompleteTextBox:(NHAutoCompleteTextField *)autoCompleteTextBox cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

}

Adjustable for the given direction of drop down.

typedef enum
{
    NHDropDownDirectionUp,
    NHDropDownDirectionDown

} NHDropDownDirection;

This control also offers some good extension functions that will help to deal with other UIs as well.

#import "UIView+NHExtension.h"
#import "UILabel+Boldify.h"
于 2015-01-03T23:00:41.383 に答える