/**
* 汉字转拼音
*
* @param aString: 需要转的字符
*/
+ (NSString *)transformToPinyin:(NSString *)aString {
//转成了可变字符串
NSMutableString * mutableString = [NSMutableString stringWithString:aString];
CFStringTransform((CFMutableStringRef) mutableString, NULL, kCFStringTransformToLatin, false);
mutableString = (NSMutableString *)[mutableString stringByFoldingWithOptions:NSDiacriticInsensitiveSearch locale:[NSLocale currentLocale]];
mutableString = [[mutableString stringByReplacingOccurrencesOfString:@" " withString:@""] mutableCopy];
return mutableString.lowercaseString;
}
self.searchTextField = [[UITextField alloc]init];
self.searchTextField.borderStyle = UITextBorderStyleNone;
self.searchTextField.textColor = [UIColor whiteColor];
self.searchTextField.placeholder = @"输入搜索内容";
self.searchTextField.delegate = self;
[self.searchTextField setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
[self.searchTextField setValue:[UIFont boldSystemFontOfSize:13] forKeyPath:@"_placeholderLabel.font"];
self.searchTextField.returnKeyType = UIReturnKeySearch;
self.searchTextField.font = [UIFont systemFontOfSize:13];
self.searchTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
[searchView addSubview:self.searchTextField];
[self.searchTextField mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(searchView.mas_left).offset(33);
make.right.equalTo(searchView.mas_right).offset(-33);
make.top.equalTo(searchBackgroundView.mas_top).offset(0);
make.bottom.equalTo(searchBackgroundView.mas_bottom).offset(0);
}];
[self.searchTextField addTarget:self
action:@selector(textFieldDidChange:)
forControlEvents:UIControlEventEditingChanged]; // 监听事件
//监听UITextField改变
- (void)textFieldDidChange:(UITextField *)sender
{
if (sender.text.length == 0) {
self.applyRecordList = self.originalList;
[self.tableView reloadData];
} else {
[self searchTraversalDisplayData:sender.text];
}
}
#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[self searchTraversalDisplayData:textField.text];
return YES;
}
-(NSMutableArray *)searchArray {
if (!_searchArray) {
_searchArray = [[NSMutableArray alloc]init];
}
return _searchArray;
}
#pragma mark - 搜索遍历
-(void)searchTraversalDisplayData:(NSString *)text {
NSString * search_pinyin = [Tools transformToPinyin:text];
NSString * search_lower = [search_pinyin lowercaseString];
[self.searchArray removeAllObjects];
dispatch_queue_t globalQueue = dispatch_get_global_queue(0, 0);
dispatch_async(globalQueue, ^{
if (text!=nil&&text.length>0) {
for (NSInteger i = 0; i<self.originalList.count; i++) {
NSDictionary * dict = self.originalList[i];
NSString * car_no = self.originalList[i][@"carno"];
NSString * car_pinyin = [Tools transformToPinyin:car_no];
NSString * car_lower = [car_pinyin lowercaseString];
if ([car_lower rangeOfString:search_lower].location != NSNotFound) {
[self.searchArray addObject:dict];
}
}
} else {
self.applyRecordList = self.originalList;
} dispatch_async(dispatch_get_main_queue(), ^{
self.applyRecordList = self.searchArray;
[self.tableView reloadData];
});
});
}