1 /**
2  *  汉字转拼音
3  *
4  *  @param aString: 需要转的字符
5  */
6 + (NSString *)transformToPinyin:(NSString *)aString {
7     //转成了可变字符串
8     NSMutableString * mutableString = [NSMutableString stringWithString:aString];
9     CFStringTransform((CFMutableStringRef) mutableString, NULL, kCFStringTransformToLatin, false);
10     mutableString = (NSMutableString *)[mutableString stringByFoldingWithOptions:NSDiacriticInsensitiveSearch locale:[NSLocale currentLocale]];
11     mutableString = [[mutableString stringByReplacingOccurrencesOfString:@” ” withString:@””] mutableCopy];
12     return mutableString.lowercaseString;
13 }

 

1     self.searchTextField = [[UITextField alloc]init];
2     self.searchTextField.borderStyle = UITextBorderStyleNone;
3     self.searchTextField.textColor = [UIColor whiteColor];
4     self.searchTextField.placeholder = @”输入搜索内容”;
5     self.searchTextField.delegate = self;
6     [self.searchTextField setValue:[UIColor whiteColor] forKeyPath:@”_placeholderLabel.textColor”];
7     [self.searchTextField setValue:[UIFont boldSystemFontOfSize:13] forKeyPath:@”_placeholderLabel.font”];
8     self.searchTextField.returnKeyType = UIReturnKeySearch;
9     self.searchTextField.font = [UIFont systemFontOfSize:13];
10     self.searchTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
11     [searchView addSubview:self.searchTextField];
12     [self.searchTextField mas_makeConstraints:^(MASConstraintMaker *make) {
13         make.left.equalTo(searchView.mas_left).offset(33);
14         make.right.equalTo(searchView.mas_right).offset(-33);
15         make.top.equalTo(searchBackgroundView.mas_top).offset(0);
16         make.bottom.equalTo(searchBackgroundView.mas_bottom).offset(0);
17     }];
18     [self.searchTextField addTarget:self
19                              action:@selector(textFieldDidChange:)
20                    forControlEvents:UIControlEventEditingChanged]; // 监听事件

 

1 //监听UITextField改变
2 – (void)textFieldDidChange:(UITextField *)sender
3 {
4     if (sender.text.length == 0) {
5         self.applyRecordList = self.originalList;
6         [self.tableView reloadData];
7     } else {
8         [self searchTraversalDisplayData:sender.text];
9     }
10 }
11 #pragma mark – UITextFieldDelegate
12 – (BOOL)textFieldShouldReturn:(UITextField *)textField
13 {
14     [self searchTraversalDisplayData:textField.text];
15     return YES;
16 }
17 -(NSMutableArray *)searchArray {
18     if (!_searchArray) {
19         _searchArray = [[NSMutableArray alloc]init];
20     }
21     return _searchArray;
22 }
23 #pragma mark – 搜索遍历
24 -(void)searchTraversalDisplayData:(NSString *)text {
25     NSString * search_pinyin = [Tools transformToPinyin:text];
26     NSString * search_lower = [search_pinyin lowercaseString];
27     [self.searchArray removeAllObjects];
28     dispatch_queue_t globalQueue = dispatch_get_global_queue(0, 0);
29     dispatch_async(globalQueue, ^{
30         if (text!=nil&&text.length>0) {
31             for (NSInteger i = 0; i<self.originalList.count; i++) {
32                 NSDictionary * dict = self.originalList[i];
33                 NSString * car_no = self.originalList[i][@”carno”];
34                 NSString * car_pinyin = [Tools transformToPinyin:car_no];
35                 NSString * car_lower = [car_pinyin lowercaseString];
36                 if ([car_lower rangeOfString:search_lower].location != NSNotFound) {
37                     [self.searchArray addObject:dict];
38                 }
39             }
40         } else {
41             self.applyRecordList = self.originalList;
42         } dispatch_async(dispatch_get_main_queue(), ^{
43             self.applyRecordList = self.searchArray;
44             [self.tableView reloadData];
45         });
46     });
47 }