技术的乐趣在于分享,欢迎多多交流,多多沟通。
全部博文(877)
分类: iOS平台
2015-10-19 23:58:12
参考http://www.cnblogs.com/syxchina/archive/2012/10/11/2720257.html
一、需要导入头文件pinyin.h这里有下载地址http://download.csdn.net/detail/u013873967/6991579
这是我写的完整代码,欢迎大家学习交流,帮我改进。http://download.csdn.net/detail/u013873967/6992825
二、ChineseString.h
1
2
3
4
5
6
7
8
9
10
11
12
|
#import <Foundation/Foundation.h>
#import "pinyin.h"
@interface ChineseString : NSObject
@property(retain,nonatomic)NSString *string;
@property(retain,nonatomic)NSString *pinYin;
//----- 返回tableview右方indexArray
+(NSMutableArray*)IndexArray:(NSArray*)stringArr;
//----- 返回联系人
+(NSMutableArray*)LetterSortArray:(NSArray*)stringArr;
|
三、ChineseString.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
#import "ChineseString.h"
@implementation ChineseString
@synthesize string;
@synthesize pinYin;
#pragma mark - 返回tableview右方 indexArray
+(NSMutableArray*)IndexArray:(NSArray*)stringArr
{
NSMutableArray *tempArray = [self ReturnSortChineseArrar:stringArr];
NSMutableArray *A_Result=[NSMutableArray array];
NSString *tempString ;
for (NSString* object in tempArray)
{
NSString *pinyin = [((ChineseString*)object).pinYin substringToIndex:1];
//不同
if (![tempString isEqualToString:pinyin])
{
// NSLog(@"IndexArray----->%@",pinyin);
[A_Result addObject:pinyin];
tempString = pinyin;
}
}
return A_Result;
}
#pragma mark - 返回联系人
+(NSMutableArray*)LetterSortArray:(NSArray*)stringArr
{
NSMutableArray *tempArray = [self ReturnSortChineseArrar:stringArr];
NSMutableArray *LetterResult=[NSMutableArray array];
NSMutableArray *item = [NSMutableArray array];
NSString *tempString ;
//拼音分组
for (NSString* object in tempArray) {
NSString *pinyin = [((ChineseString*)object).pinYin substringToIndex:1];
NSString *string = ((ChineseString*)object).string;
//不同
if (![tempString isEqualToString:pinyin])
{
//分组
item = [NSMutableArray array];
[item addObject:string];
[LetterResult addObject:item];
//遍历
tempString = pinyin;
} else //相同
{
[item addObject:string];
}
}
return LetterResult;
}
//过滤指定字符串 里面的指定字符根据自己的需要添加
+(NSString*)RemoveSpecialCharacter: (NSString *)str {
NSRange urgentRange = [str rangeOfCharacterFromSet: [NSCharacterSet characterSetWithCharactersInString: @ ",.?、 ~¥#&<>《》()[]{}【】^@/£¤|§¨「」『』¢¬ ̄~@#&*()——+|《》$_?" ]];
if (urgentRange.location != NSNotFound)
{
return [self RemoveSpecialCharacter:[str stringByReplacingCharactersInRange:urgentRange withString:@ "" ]];
}
return str;
}
///////////////////
//
//返回排序好的字符拼音
//
///////////////////
+(NSMutableArray*)ReturnSortChineseArrar:(NSArray*)stringArr
{
//获取字符串中文字的拼音首字母并与字符串共同存放
NSMutableArray *chineseStringsArray=[NSMutableArray array];
for ( int i=0;i<[stringArr count];i++)
{
ChineseString *chineseString=[[ChineseString alloc]init];
chineseString.string=[NSString stringWithString:[stringArr objectAtIndex:i]];
if (chineseString.string==nil){
chineseString.string=@ "" ;
}
//去除两端空格和回车
chineseString.string = [chineseString.string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
//此方法存在一些问题 有些字符过滤不了
//NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:@"@/:;()¥「」"、[]{}#%-*+=_\\|~<>$?^?'@#$%^&*()_+'\""];
//chineseString.string = [chineseString.string stringByTrimmingCharactersInSet:set];
//这里我自己写了一个递归过滤指定字符串 RemoveSpecialCharacter
chineseString.string =[ChineseString RemoveSpecialCharacter:chineseString.string];
// NSLog(@"string====%@",chineseString.string);
//判断首字符是否为字母
NSString *regex = @ "[A-Za-z]+" ;
NSPredicate*predicate = [NSPredicate predicateWithFormat:@ "SELF MATCHES %@" ,regex];
if ([predicate evaluateWithObject:chineseString.string])
{
//首字母大写
chineseString.pinYin = [chineseString.string capitalizedString] ;
} else {
if (![chineseString.string isEqualToString:@ "" ]){
NSString *pinYinResult=[NSString string];
for ( int j=0;j<chineseString.string.length;j++){
NSString *singlePinyinLetter=[[NSString stringWithFormat:@ "%c" ,pinyinFirstLetter([chineseString.string characterAtIndex:j])]uppercaseString];
pinYinResult=[pinYinResult stringByAppendingString:singlePinyinLetter];
}
chineseString.pinYin=pinYinResult;
} else {
chineseString.pinYin=@ "" ;
}
}
[chineseStringsArray addObject:chineseString];
}
//按照拼音首字母对这些Strings进行排序
NSArray *sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@ "pinYin" ascending:YES]];
[chineseStringsArray sortUsingDescriptors:sortDescriptors];
return chineseStringsArray;
}
|
四、UITableViewController.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
- ( void )viewDidLoad
{
self.title = @ "我的好友" ;
[super viewDidLoad];
NSArray *stringsToSort=[NSArray arrayWithObjects:
@ "¥hhh, .$" ,@ " ¥Chin ese " ,@ "开源中国 " ,@ "www.oschina.net" ,
@ "开源技术" ,@ "社区" ,@ "开发者" ,@ "传播" ,
@ "2013" ,@ "100" ,@ "中国" ,@ "暑假作业" ,
@ "键盘" , @ "鼠标" ,@ "hello" ,@ "world" ,
nil];
self.indexArray = [ChineseString IndexArray:stringsToSort];
self.LetterResultArr = [ChineseString LetterSortArray:stringsToSort];
}
|