iOS 索引功能

2018-12-26  本文已影响0人  sheldon_龙

简介
UITableView索引功能是常见的,主要是获取中英文的首字母并排序,系统自带获取首字母

//系统获取首字母

- (NSString *) pinyinFirstLetter:(NSString*)sourceString {
NSMutableString *source = [sourceString mutableCopy];

CFStringTransform((__bridge CFMutableStringRef)source, NULL, kCFStringTransformMandarinLatin, NO);

CFStringTransform((__bridge CFMutableStringRef)source, NULL, kCFStringTransformStripDiacritics, NO);//这一行是去声调的

return source;
}

类似获取拼音的第三方框架

TableView索引栏相关设置

#pragma mark---tableView索引相关设置----

//添加TableView头视图标题

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{

NSDictionary *dict = self.dataArray[section];

NSString *title = dict[@"firstLetter"];

 return title;
}

//添加索引栏标题数组
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
NSMutableArray *resultArray =[NSMutableArray arrayWithObject:UITableViewIndexSearch];

for(NSDictionary *dict in self.dataArray) {
    
    NSString *title = dict[@"firstLetter"];
    
    [resultArray addObject:title];
}

return resultArray;
}


//点击索引栏标题时执行
- (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 添加了搜索标识
}
}
上一篇 下一篇

猜你喜欢

热点阅读