ios 模糊搜索支持首字母搜索、拼音搜索、汉字搜索

/**
 *  汉字转拼音
 *
 *  @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];
        });
    });
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值