美文网首页
UITableView添加索引栏

UITableView添加索引栏

作者: 无法触碰 | 来源:发表于2016-12-08 09:29 被阅读68次

#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>

相关文章

网友评论

      本文标题:UITableView添加索引栏

      本文链接:https://www.haomeiwen.com/subject/ojnjmttx.html