代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
ObjC
】
iOS索引搜索核心代码
作者:
shanxi_liyong
/ 发布于
2015/11/12
/
1479
/************************************************************ * * EaseMob CONFIDENTIAL * __________________ * Copyright (C) 2013-2014 EaseMob Technologies. All rights reserved. * * NOTICE: All information contained herein is, and remains * the property of EaseMob Technologies. * Dissemination of this information or reproduction of this material * is strictly forbidden unless prior written permission is obtained * from EaseMob Technologies. */ #import <UIKit/UIKit.h> @interface ChineseToPinyin : NSObject { } + (NSString *) pinyinFromChineseString:(NSString *)string; + (char) sortSectionTitle:(NSString *)string; @end #import "ViewController.h" #import "ChineseToPinyin.h" @interface ViewController () { NSMutableArray *sectionTitleArray; NSMutableArray *groupListDataSource; } @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; sectionTitleArray = [[NSMutableArray alloc]init]; groupListDataSource = [[NSMutableArray alloc]init]; [sectionTitleArray addObject:@"q"]; NSArray *array = @[@"a",@"b",@"c",@"d",@"e",@"f",@"g"]; [self sortDataArray:array]; } - (void)sortDataArray:(NSArray *)dataArray { //建立索引的核心 UILocalizedIndexedCollation *indexCollation = [UILocalizedIndexedCollation currentCollation]; NSArray *sectionTitles = [NSArray arrayWithArray:[indexCollation sectionTitles]]; //返回27,是a-z和# NSInteger highSection = [sectionTitles count]; //tableView 会被分成27个section NSMutableArray *sortedArray = [NSMutableArray arrayWithCapacity:highSection]; for (int i = 0; i <= highSection; i++) { NSMutableArray *sectionArray = [NSMutableArray arrayWithCapacity:1]; [sortedArray addObject:sectionArray]; } //名字分section for (NSString *model in dataArray) { //getUserName是实现中文拼音检索的核心,见NameIndex类 NSString *firstLetter = [ChineseToPinyin pinyinFromChineseString:model]; NSInteger section = [indexCollation sectionForObject:[firstLetter substringToIndex:1] collationStringSelector:@selector(uppercaseString)]; NSMutableArray *array = [sortedArray objectAtIndex:section]; [array addObject:model]; } //每个section内的数组排序 for (int i = 0; i < [sortedArray count]; i++) { NSArray *array = [[sortedArray objectAtIndex:i] sortedArrayUsingComparator:^NSComparisonResult(NSString *obj1, NSString *obj2) { NSString *firstLetter1 = [ChineseToPinyin pinyinFromChineseString:obj1]; firstLetter1 = [[firstLetter1 substringToIndex:1] uppercaseString]; NSString *firstLetter2 = [ChineseToPinyin pinyinFromChineseString:obj2]; firstLetter2 = [[firstLetter2 substringToIndex:1] uppercaseString]; return [firstLetter1 caseInsensitiveCompare:firstLetter2]; }]; [sortedArray replaceObjectAtIndex:i withObject:[NSMutableArray arrayWithArray:array]]; } for (int i = 0; i < [sectionTitles count]; i++) { if ([[sortedArray objectAtIndex:i] count] > 0) { NSLog(@"%@",[sectionTitles objectAtIndex:i]); [sectionTitleArray addObject:[sectionTitles objectAtIndex:i]]; [groupListDataSource addObject:[sortedArray objectAtIndex:i]]; } } } - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView { return sectionTitleArray; } #pragma mark - UITableViewDataSource -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return groupListDataSource.count; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [[groupListDataSource objectAtIndex:section] count]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
试试其它关键字
索引搜索
同语言下
.
根据生日计算星座
.
精简的实现一个内存池
.
iOS索引搜索核心代码
.
iOS 搜索框
.
文件操作
.
UIWebView 加载本地网页
.
取消导航条对视图的影响
.
利用终端显示 隐藏文件
.
更改导航条背景颜色
.
iOS 字体斜体
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
shanxi_liyong
贡献的其它代码
(
10
)
.
iOS索引搜索核心代码
.
词法分析器
.
循环创建目录
.
freemark实现,简单的替换
.
计算斐波那契数列
.
字符串切片,拼接字符串处理文本
.
图片添加水印和压缩
.
图片延迟加载
.
javaBean中根据方法名获取属性名
.
复制属性的值
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3