扩展了一下 搜索框,能够实现拼音和首字母模糊搜索

基本搜索 上一篇文章

#import "NSString+utility.h"

@interface WJWPinyinSearchViewController ()<UISearchResultsUpdating,UITableViewDelegate,UITableViewDataSource,UISearchBarDelegate>

@property (nonatomic, strong) UITableViewController *searchTableViewController;
@property (nonatomic, strong) UISearchController *searchController;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) NSMutableArray *searchList; @end @implementation WJWPinyinSearchViewController - (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor]; [self addChildViewController:self.searchTableViewController];
[self initDataArray];
[self configUI];
} - (void)initDataArray{
[self.dataArray addObject:@"世界"];
[self.dataArray addObject:@"核平"];
[self.dataArray addObject:@"平行"];
[self.dataArray addObject:@"宇宙"];
[self.dataArray addObject:@"技术进步"];
[self.dataArray addObject:@"毫无意义"];
[self.dataArray addObject:@"尘归尘,土归土"];
[self.dataArray addObject:@"中"];
[self.dataArray addObject:@"国"];
[self.dataArray addObject:@"红"]; } - (void)configUI { [self.view addSubview: self.searchTableViewController.tableView];
self.searchTableViewController.tableView.frame = self.view.frame;
} #pragma mark ---UITableViewDataSource---
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
} - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (self.searchController.active) {
return [self.searchList count];
}else {
return self.dataArray.count;
}
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *searchCellId = @"SearchCellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:searchCellId];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:searchCellId];
}
if (self.searchController.active) {
[cell.textLabel setText:self.searchList[indexPath.row]];
}else {
[cell.textLabel setText:self.dataArray[indexPath.row]];
}
return cell;
} #pragma mark ----UISearchResultsUpdating---- - (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
NSString *searchString = [self.searchController.searchBar text];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[c] %@", searchString];
if (self.searchList != nil) {
[self.searchList removeAllObjects];
}
//过滤数据
self.searchList = [NSMutableArray arrayWithArray:[_dataArray filteredArrayUsingPredicate:predicate]];
//刷新表格
[self.searchTableViewController.tableView reloadData];
} - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
NSLog(@"输入字符串为:%@ -- %lu", searchText, (unsigned long)searchText.length);
//需要事先清空存放搜索结果的数组
[self.searchList removeAllObjects]; //在子线程中做搜索操作
dispatch_queue_t globalQueue = dispatch_get_global_queue(0, 0);
dispatch_async(globalQueue, ^{
if (searchText != nil && searchText.length > 0) {
for (NSString *tempStr in self.dataArray) {
NSString *pinyin = [tempStr transformToPinyin:tempStr];
NSLog(@"字符拼音:%@",pinyin);
if ([pinyin rangeOfString:searchText options:NSCaseInsensitiveSearch].length > 0) {
[self.searchList addObject:tempStr];
}
}
}else {
self.searchList = [NSMutableArray arrayWithArray:self.dataArray];
}
//回到主线程刷新表格
dispatch_async(dispatch_get_main_queue(), ^{
[self.searchTableViewController.tableView reloadData];
});
});
} -(UISearchController *)searchController {
if (!_searchController) {
_searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
_searchController.searchResultsUpdater = self;
_searchController.dimsBackgroundDuringPresentation = NO;
_searchController.hidesNavigationBarDuringPresentation = YES;//搜索框编辑时隐藏导航栏
_searchController.searchBar.frame = CGRectMake(self.searchController.searchBar.frame.origin.x, _searchController.searchBar.frame.origin.y, _searchController.searchBar.frame.size.width, 44.0);
self.searchTableViewController.tableView.tableHeaderView = _searchController.searchBar;
_searchController.searchBar.delegate = self;
}
return _searchController;
} - (NSMutableArray *)dataArray {
if (!_dataArray) {
_dataArray = [NSMutableArray array];
}
return _dataArray;
} - (UITableViewController *)searchTableViewController {
if (!_searchTableViewController) {
_searchTableViewController = [[UITableViewController alloc] init];
_searchTableViewController.tableView.delegate = self;
_searchTableViewController.tableView.dataSource = self;
}
return _searchTableViewController;
} /*
#pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/ @end

"NSString+utility.h" 中有一个方法用来将字符转为拼音字符串

/**
把汉字字符串转为拼音 @param aString 汉字字符串
@return 拼音字符串
*/
- (NSString *)transformToPinyin:(NSString *)aString
{
//转成了可变字符串
NSMutableString *str = [NSMutableString stringWithString:aString];
CFStringTransform((CFMutableStringRef)str,NULL, kCFStringTransformMandarinLatin,NO); //再转换为不带声调的拼音
CFStringTransform((CFMutableStringRef)str,NULL, kCFStringTransformStripDiacritics,NO);
NSArray *pinyinArray = [str componentsSeparatedByString:@" "];
NSMutableString *allString = [NSMutableString new]; int count = 0; for (int i = 0; i < pinyinArray.count; i++)
{
for(int i = 0; i < pinyinArray.count;i++)
{
if (i == count) {
[allString appendString:@"#"];
//区分第几个字母
}
[allString appendFormat:@"%@",pinyinArray[i]];
}
[allString appendString:@","];
count ++;
}
NSMutableString *initialStr = [NSMutableString new];
//拼音首字母
for (NSString *s in pinyinArray)
{
if (s.length > 0)
{
[initialStr appendString: [s substringToIndex:1]];
}
}
[allString appendFormat:@"#%@",initialStr];
[allString appendFormat:@",#%@",aString];
return allString;
}

iOS拼音搜索,拼音首字母搜索的更多相关文章

  1. SQL汉字转拼音函数-支持首字母、全拼

    SQL汉字转拼音函数-支持首字母.全拼 FROM :http://my.oschina.net/ind/blog/191659 作者不详 --方法一sqlserver汉字转拼音首字母 --调用方法 s ...

  2. C#汉字转拼音(npinyin)将中文转换成拼音全文或首字母

    汉字转拼音貌似一直是C#开发的一个难题,无论什么方案都有一定的bug,之前使用了两种方案. 1.Chinese2Spell.cs 一些不能识别的汉字全部转为Z 2.Microsoft Visual S ...

  3. 【Solr】 solr对拼音搜索和拼音首字母搜索的支持

    问:对于拼音和拼音首字母的支持,当你在搜商品的时候,如果想输入拼音和拼音首字母就给出商品的信息,怎么办呢? 实现方式有2种,但是他们其实是对应的.  用lucene实现 1.建索引, 多建一个索引字段 ...

  4. MVC+Jquery+autocomplete(汉字||拼音首字母搜索)

    最近项目中用到了autocomplete了,总结一下经验. 我们先来看一下效果:

  5. jQuery 实现前端模糊匹配与首字母搜索

    实现效果 源码 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <t ...

  6. Lucene + Pinyin4J 提供首字母搜索(——)

    遇到一个集团需求,要求在地址查询时候提供拼音搜索,第一反应应该不难,不过实现过程中却一波三折. 1.第一步是讲字段首字母进行索引,具体可以使用Pinyin4j提供的方法完成. 2.原来系统用的luce ...

  7. java汉字转拼音以及得到首字母通用方法

    package oa.common.utils;   import net.sourceforge.pinyin4j.PinyinHelper; import net.sourceforge.piny ...

  8. Java获取中文拼音、中文首字母缩写和中文首字母

    获取中文拼音(如:广东省 -->guangdongsheng) /** * 得到中文全拼 * @param src 需要转化的中文字符串 * @return */ public static S ...

  9. vue集成汉字转拼音或提取首字母

    需求:             有时我们为了节省用户的维护量,需要根据中文生成出相应的拼音和缩写 解决:            此方法是利用汉字和Unicode编码对应找到相应字母 一.编写汉字和编码 ...

随机推荐

  1. 第二章 Java程序设计环境

    安装 Java 开发工具包 JDK : 编写Java程序的程序员使用的软件 JRE : 运行Java程序的环境,包含JVM和基本类库, 但不包含编译器 SE, EE, ME Java FX : 用于图 ...

  2. JSP标签和JSTL

    Java的5个标签库:核心(c).格式化(fmt).函数(fn).SQL(sql).XML(x) SQL.XML库不推荐使用 核心标签库(c) //taglib指令 <%@ taglib pre ...

  3. vue路由守卫(全局守卫)

    router.beforeEach((to,from,next)=>{}) 回调函数中的参数, to:进入到哪个路由去, from:从哪个路由离开, next:函数,决定是否展示你要看到的路由页 ...

  4. __name__的意义与作用

    首先定义了一个test.py的文件,然后再定义一个函数,并在函数定义后直接运行: test.py def HaveFun():  if __name__ == '__main__':  print(' ...

  5. [原创]FPGA JTAG工具设计(一)

    先来看不同JTAG方案,下载配置QSPI Flash所耗时间 基于FTDI方案,JTAG下载时间为494sec JTAG chain configuration ------------------- ...

  6. RedHat 6配置yum源为网易镜像(转)

    概述 由于版权的问题,RedHat6不能直接使用yum一些指令,需要配置yum源为网易镜像,但是网上谈到很多:整理一下,将有用的信息整理如下,以便于能够为其他的配置服务配置使用:需要卸载掉原理系统自带 ...

  7. Eigen::Matrix与array数据转换

    1. 数组转化为Eigen::Matrix ]; cout << "colMajor matrix = \n" << Map<Matrix3i> ...

  8. Spring注解式事务解析

    #Spring注解式事务解析 增加一个Advisor 首先往Spring容器新增一个Advisor,BeanFactoryTransactionAttributeSourceAdvisor,它包含了T ...

  9. Mac 常用的快捷键

    Mac 菜单和键盘通常对某些按键使用符号,其中包括以下修饰键: Command(或 Cmd)⌘ Shift ⇧ Option(或 Alt)⌥ Control(或 Ctrl)⌃ Caps Lock ⇪ ...

  10. asp+SqlServer2008开发【第三集:win2winSSH远程连接—像连接Linux一样操作】

    1,参考:https://blog.csdn.net/flyingshuai/article/details/72897692 和https://blog.csdn.net/nijiayy/artic ...