我有一个人员列表,用户应该选择一个。大约有100人,因此用户滚动UIPickerView
会很痛苦。我需要让用户能够键入一些字母以减少选择的数量。许多环境都像文本框那样具有下拉控件,因此您可以通过键入来过滤值。在iOS中如何做到这一点?
我使用核心数据
答案 0 :(得分:1)
在标头文件中链接<UITextFieldDelegate>
或<UISearchBarDelegate>
。将您的UITextField
或UISearchBar
代表设为自己。
添加其委托方法:
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
或
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;
并调用您自己的核心数据搜索方法:
- (void)searchFor:(NSString *)searchString
{
NSEntityDescription *entity = [NSEntityDescription entityForName:@"MyEntity" inManagedObjectContext:_managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:entity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"wHaTeVeR contains[c] %@", searchString];
[fetchRequest setPredicate:predicate];
NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"wHaTeVeR" ascending:YES];
[fetchRequest setSortDescriptors:@[sort]];
NSError *fetchError = nil;
NSArray *objects = [_managedObjectContext executeFetchRequest:fetchRequest error:&fetchError];
这将查找包含searchString的单词。希望这会引导您朝着正确的方向前进。 :)