0

UITextFieldの助けを借りて、からのテキストでフィルタリングできるリストを実装していますUITextFieldDelegate

コードは次のとおりです。

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

    _tempWrittenText = [textField.text stringByReplacingCharactersInRange: range withString: string];
    [self filterCountries];

    return YES;
}

/** Filter the countries with the currently typed text */
- (void) filterCountries {
    if (_tempWrittenText.length == 0) {
        _visibleCountriesList = (NSMutableArray*) _countriesList;
        [tableMedals reloadSections: [NSIndexSet indexSetWithIndex: 0] withRowAnimation: UITableViewRowAnimationNone];

    } else {
        _visibleCountriesList = [NSMutableArray array];

        for (Country* c in _countriesList) {
            if ([c.name rangeOfString: _tempWrittenText].location != NSNotFound) {
                [_visibleCountriesList addObject: c];
            }
        }
        [tableMedals reloadSections: [NSIndexSet indexSetWithIndex: 0] withRowAnimation: UITableViewRowAnimationFade];
    }

}

フィルタは、テキストを入力するときに完全に機能します。ただし、このメソッドは、 DONE- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)stringキーボードキーが押されたときにも起動され(キーボードを非表示にするため)、すべてのアイテムがそのままではなく、奇妙に消去されます。

問題は、rangeOfStringパーツが常にNSNotFoundを返すことです。変数をログに記録すると、2つの文字列が正しい理由がわかりません。

例: [@"Angola" rangeOfString @"A"].location NSNotFoundを提供します

繰り返しますが、これはキーボードが非表示になっている場合にのみ発生します。誰かアイデアがありますか?

前もって感謝します

4

2 に答える 2

1

The

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

完了ボタンが押されても起動されません。それ以外の場合は、この関数を強制的に呼び出す必要があります。

これを使用してキーボードを非表示にします。これは上記のメソッドをトリガーしません。

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}
于 2012-06-07T21:16:33.770 に答える
0

Doneキーを押すと、システムが改行文字を追加することがわかりました。それがデフォルトの動作であるか、なぜそうなっているのかわかりませんでした。

これを防ぐ方法は、以下を追加することです。

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    return YES;
}

それが誰かを助けることを願っています。

于 2012-06-07T21:40:59.297 に答える