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

基本搜索 上一篇文章

#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. Windows下VSCode编译调试c/c++

    参考链接:  https://blog.csdn.net/c_duoduo/article/details/51615381 支持makefile编译: https://www.cnblogs.com ...

  2. Hough transform(霍夫变换)

    主要内容: 1.Hough变换的算法思想 2.直线检测 3.圆.椭圆检测 4.程序实现 一.Hough变换简介 Hough变换是图像处理中从图像中识别几何形状的基本方法之一.Hough变换的基本原理在 ...

  3. HDR拍照

    HDR 拍照:        (High Dynamic Range Imaging)高动态范围成像,是用来实现比普通数字图像技术更大曝光动态范围(即更大的明暗差别)的一组技术.高动态范围成像的目的就 ...

  4. 【原创】大叔问题定位分享(24)hbase standalone方式启动报错

    hbase 2.0.2 hbase standalone方式启动报错: 2019-01-17 15:49:08,730 ERROR [Thread-24] master.HMaster: Failed ...

  5. 【原创】大叔问题定位分享(21)spark执行insert overwrite非常慢,比hive还要慢

    最近把一些sql执行从hive改到spark,发现执行更慢,sql主要是一些insert overwrite操作,从执行计划看到,用到InsertIntoHiveTable spark-sql> ...

  6. python计算文件的行数的方法

    1.简单方法把文件读入一个大的列表中,然后统计列表的长度.   count = len(open("文件名").readlines()) print count 2.读取文件某一行 ...

  7. Selenium+Python ---- 免登录

    1.免登录在进行测试的过程中难免会遇到登录的情况,给测试工作添加了工作量,本文仅提供一些思路供参考解决方式:手动请求中添加cookies.火狐的profile文件记录信息实现.人工介入.万能验证码.去 ...

  8. 一分30秒 kali 开机显示 a start job is running for dev-disk 处理

    在给kali虚拟机扩容后,使用fdisk  /dev/sda  更改新建分区后,重启系统出现一分30秒等待. 解决方案:    (root 权限) 第一步:sudo blkid (查看当前系统的真实的 ...

  9. PHP中的面向对象思想

    <?php header("Content-Type: text/html; charset=gb2312"); class person{ /** * 成员属性 * 在类中 ...

  10. leetcode刷题第一日<两数和问题>

    开始就用到了c++的哈希表是真的恶心,首先学习一波基础知识 https://blog.csdn.net/u010025211/article/details/46653519 下面放下大佬的代码 cl ...