iOS拼音搜索,拼音首字母搜索
扩展了一下 搜索框,能够实现拼音和首字母模糊搜索
基本搜索 上一篇文章
#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拼音搜索,拼音首字母搜索的更多相关文章
- SQL汉字转拼音函数-支持首字母、全拼
SQL汉字转拼音函数-支持首字母.全拼 FROM :http://my.oschina.net/ind/blog/191659 作者不详 --方法一sqlserver汉字转拼音首字母 --调用方法 s ...
- C#汉字转拼音(npinyin)将中文转换成拼音全文或首字母
汉字转拼音貌似一直是C#开发的一个难题,无论什么方案都有一定的bug,之前使用了两种方案. 1.Chinese2Spell.cs 一些不能识别的汉字全部转为Z 2.Microsoft Visual S ...
- 【Solr】 solr对拼音搜索和拼音首字母搜索的支持
问:对于拼音和拼音首字母的支持,当你在搜商品的时候,如果想输入拼音和拼音首字母就给出商品的信息,怎么办呢? 实现方式有2种,但是他们其实是对应的. 用lucene实现 1.建索引, 多建一个索引字段 ...
- MVC+Jquery+autocomplete(汉字||拼音首字母搜索)
最近项目中用到了autocomplete了,总结一下经验. 我们先来看一下效果:
- jQuery 实现前端模糊匹配与首字母搜索
实现效果 源码 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <t ...
- Lucene + Pinyin4J 提供首字母搜索(——)
遇到一个集团需求,要求在地址查询时候提供拼音搜索,第一反应应该不难,不过实现过程中却一波三折. 1.第一步是讲字段首字母进行索引,具体可以使用Pinyin4j提供的方法完成. 2.原来系统用的luce ...
- java汉字转拼音以及得到首字母通用方法
package oa.common.utils; import net.sourceforge.pinyin4j.PinyinHelper; import net.sourceforge.piny ...
- Java获取中文拼音、中文首字母缩写和中文首字母
获取中文拼音(如:广东省 -->guangdongsheng) /** * 得到中文全拼 * @param src 需要转化的中文字符串 * @return */ public static S ...
- vue集成汉字转拼音或提取首字母
需求: 有时我们为了节省用户的维护量,需要根据中文生成出相应的拼音和缩写 解决: 此方法是利用汉字和Unicode编码对应找到相应字母 一.编写汉字和编码 ...
随机推荐
- 两个Html页面之间值得传递
传值的页面:<a href='stockProductInfo.html?prdId="+v.prdID+"' target='_blank'></html> ...
- linux tomcat单机部署多应用
1.修改/etc/profile 增加tomcat环境变量
- MySql常见的数据类型
⒈整型 名称 字节数 tinyint 1 smallint 2 mediumint 3 int/integer 4 bigint 8 特点: 1.如果不设置无符号还是有符号,默认是有符号,如果想设置无 ...
- 帆软报表(finereport)常用函数
1. SUM SUM(number1,number2,…):求一个指定单元格区域中所有数字之和.Number1,number2,…:1到30个参数或指定单元格区域中所有数字. 注: 函数将直接键入参数 ...
- php curl使用
- add web server(nginx+apache)
#!/bin/bash # # Web Server Install Script # Last Updated 2012.09.24 # ##### modify by WanJie 2012.09 ...
- Python学习(四十)—— Djago之认证系统
一.COOKIE 与 SESSION 概念 cookie不属于http协议范围,由于http协议无法保持状态,但实际情况,我们却又需要“保持状态”,因此cookie就是在这样一个场景下诞生. cook ...
- Beta(3/7)
鐵鍋燉腯鱻 项目:小鱼记账 团队成员 项目燃尽图 冲刺情况描述 站立式会议照片 各成员情况 团队成员 学号 姓名 git地址 博客地址 031602240 许郁杨 (组长) https://githu ...
- 原生JavaScript中动画与特效的实现原理
现如今,许多页面上均有一些动画效果.适当的动画效果可以在一定程度上提高页面的美观度,具有提示效果的动画可以增强页面的易用性. 实现页面动画的途径一般有两种. 一种是通过操作JavaScript间接操作 ...
- Codeforces.871D.Paths(莫比乌斯反演 根号分治)
题目链接 \(Description\) 给定\(n\),表示有一张\(n\)个点的无向图,两个点\(x,y\)之间有权值为\(1\)的边当且仅当\(\gcd(x,y)\neq1\).求\(1\sim ...