码迷,mamicode.com
首页 > 其他好文 > 详细

UITextField中文搜索

时间:2015-05-29 20:18:50      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:

导入头文件

#import "ChineseInclude.h"
#import "PinYinForObjc.h"

    NSMutableArray *searchResults;//可变数组,添加搜索到的数据

设置数据源

- (NSArray *)dataArray {
    if (!_dataArray) {
        _dataArray = @[@"百度",@"六六",@"谷歌",@"苹果",@"and",@"table",@"view",@"and",@"and",@"苹果IOS",@"谷歌android",@"微软",@"微软WP",@"table",@"table",@"table",@"六六",@"六六",@"六六",@"table",@"table",@"table"];
    }
    return _dataArray;
}

监听textField

   [_textField addTarget:self action:@selector(textFieldEditChanged:) forControlEvents:UIControlEventEditingChanged];

当输入的时候进行搜索

- (void)textFieldEditChanged:(UITextField *)mySearchBar
{
    searchResults = [[NSMutableArray alloc]init];
    if (mySearchBar.text.length>0&&![ChineseInclude isIncludeChineseInString:mySearchBar.text]) {
        for (int i=0; i<self.dataArray.count; i++) {
            if ([ChineseInclude isIncludeChineseInString:self.dataArray[i]]) {
                NSString *tempPinYinStr = [PinYinForObjc chineseConvertToPinYin:self.dataArray[i]];
                NSRange titleResult=[tempPinYinStr rangeOfString:mySearchBar.text options:NSCaseInsensitiveSearch];
                if (titleResult.length>0) {
                    [searchResults addObject:self.dataArray[i]];
                }
                
//                NSString *tempPinYinHeadStr = [PinYinForObjc chineseConvertToPinYinHead:self.dataArray[i]];
//                NSRange titleHeadResult=[tempPinYinHeadStr rangeOfString:mySearchBar.text options:NSCaseInsensitiveSearch];
//                if (titleHeadResult.length>0) {
//                    [searchResults addObject:self.dataArray[i]];
//                }
            }
            else {
                NSRange titleResult=[self.dataArray[i] rangeOfString:mySearchBar.text options:NSCaseInsensitiveSearch];
                if (titleResult.length>0) {
                    [searchResults addObject:self.dataArray[i]];
                }
            }
        }
    } else if (mySearchBar.text.length>0&&[ChineseInclude isIncludeChineseInString:mySearchBar.text]) {
        for (NSString *tempStr in self.dataArray) {
            NSRange titleResult=[tempStr rangeOfString:mySearchBar.text options:NSCaseInsensitiveSearch];
            if (titleResult.length>0) {
                [searchResults addObject:tempStr];
            }
        }
    }
    NSLog(@"搜索结果:%@",searchResults);
    //在这里刷新   // [_tableView reloadData]; }

效果图:

技术分享技术分享

Demo下载链接:http://pan.baidu.com/s/1bncyjZD

 

UITextField中文搜索

标签:

原文地址:http://www.cnblogs.com/hxwj/p/4539245.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!