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を提供します
繰り返しますが、これはキーボードが非表示になっている場合にのみ発生します。誰かアイデアがありますか?
前もって感謝します