#pragma mark---tableView索引相關設置----
<pre>
- (NSString *) pinyinFirstLetter:(NSString*)sourceString {
NSMutableString *source = [sourceString mutableCopy];
CFStringTransform((__bridge CFMutableStringRef)source, NULL, kCFStringTransformMandarinLatin, NO);
CFStringTransform((__bridge CFMutableStringRef)source, NULL, kCFStringTransformStripDiacritics, NO);//這一行是去聲調的
return source;
}
</pre>
//添加索引欄標題數組
<pre>
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
NSMutableArray *resultArray =[NSMutableArray arrayWithObject:UITableViewIndexSearch];
for (NewModel * dict in self.userArray) {
NSString *title = [dict.nick_name substringWithRange:NSMakeRange(0, 1)];
NSString * newtitle = [title uppercaseString];
[resultArray addObject:newtitle];
}
//
return resultArray;
}
</pre>
//點擊索引欄標題時執行
<pre>
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
//這里是為了指定索引index對應的是哪個section的,默認的話直接返回index就好。其他需要定制的就針對性處理
if ([title isEqualToString:UITableViewIndexSearch])
{
[tableView setContentOffset:CGPointZero animated:NO];//tabview移至頂部
return NSNotFound;
}
else
{
return [[UILocalizedIndexedCollation currentCollation] sectionForSectionIndexTitleAtIndex:index] - 1; // -1 添加了搜索標識
}
}
</pre>