实现中文下的UITableView Index

遇到的技术并解决之:

1.实现中文下的tableView index:

   UILocalizedIndexedCollation* theCollation = [UILocalizedIndexedCollationcurrentCollation];

   NSInteger sect = [theCollation sectionForObject:per collationStringSelector:@selector(getname)];

   per.sectionNumber = sect;

代码里根据person对象per的name属性来进行排序,per的sectionNumber记录的是name 占az和#的第几个,貌似用6.0的模拟器能直接排序,但是到真机上就不行了,5.0的模拟器也不行

所以网上有查了查方法 要将中文转为拼音 在排序就好了   引入逍遥一狂的pinyin文件 

-(NSString*)getname

{

    if ([namecanBeConvertedToEncoding: NSASCIIStringEncoding]) {//如果是英语  

        return name;  

    }  

    else { //如果是非英语  

        return [NSStringstringWithFormat:@"%c",pinyinFirstLetter([namecharacterAtIndex:0])];  

    }  

}

就能得到了英文的,根据英文 就能根据上面的那个文件进行排序了

参考链接:http://blog.sina.com.cn/s/blog_6297d52d01010mbe.html

原文地址:https://www.cnblogs.com/neworiginou/p/2817525.html