iOS 使用UILocalizedIndexedCollatio
原文链接:http://www.cnblogs.com/pretty-guy/p/4831586.html
UITableView在行数相当多的时候,给人的感觉是非常笨重的。通常为了方便用户使用,采用的方法有:搜索框、按层级展示、区域索引标题。
前两种就不用介绍了,此文就介绍区域索引标题的实现。
区域索引标题可以在通讯录里看到,类似这样: image区域索引标题可以通过转拼音实现,本文主要介绍使用UILocalizedIndexedCollation实现区域索引标题
UILocalizedIndexedCollation是苹果贴心为开发者提供的排序工具,会自动根据不同地区生成索引标题
<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">//根据SEL方法返回的字符串判断对象应该处于哪个分区
- (NSInteger)sectionForObject:(id)object collationStringSelector:(SEL)selector; //根据SEL方法返回的string对数组元素排序
- (NSArray *)sortedArrayFromArray:(NSArray *)array collationStringSelector:(SEL)selector;</pre>
具体使用方法如下:
1.构造数据源
[ 复制代码](javascript:void(0); "复制代码")
<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">NSArray *testArr = @[@"悟空",@"沙僧",@"八戒", @"吴进", @"悟能", @"唐僧", @"诸葛亮", @"赵子龙",@"air", @"Asia", @"crash", @"basic", @"阿里郎"];
NSMutableArray *personArr = [NSMutableArray arrayWithCapacity:testArr.count]; for (NSString *str in testArr) {
Person *person = [[Person alloc] initWithName:str];
[personArr addObject:person];
}
UILocalizedIndexedCollation *collation = [UILocalizedIndexedCollation currentCollation];
NSLog(@"%@", collation.sectionTitles); //1.获取获取section标题
NSArray *titles = collation.sectionTitles; //2.构建每个section数组
NSMutableArray *secionArray = [NSMutableArray arrayWithCapacity:titles.count]; for (int i = 0; i < titles.count; i++) {
NSMutableArray *subArr = [NSMutableArray array];
[secionArray addObject:subArr];
} //3.排序 //3.1 按照将需要排序的对象放入到对应分区数组
for (Person *person in personArr) {
NSInteger section = [collation sectionForObject:person collationStringSelector:@selector(name)];
NSMutableArray *subArr = secionArray[section];
[subArr addObject:person];
} //3.2 分别对分区进行排序
for (NSMutableArray *subArr in secionArray) {
NSArray *sortArr = [collation sortedArrayFromArray:subArr collationStringSelector:@selector(name)];
[subArr removeAllObjects];
[subArr addObjectsFromArray:sortArr];
}</pre>
[
复制代码
](javascript:void(0); "复制代码")
2.实现TableViewDataSource
[ 复制代码](javascript:void(0); "复制代码")
<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">#pragma mark SectionTitles
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{ return [[[UILocalizedIndexedCollation currentCollation] sectionTitles] objectAtIndex:section];
} - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{ return [[UILocalizedIndexedCollation currentCollation] sectionIndexTitles];
} - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{ return [[UILocalizedIndexedCollation currentCollation] sectionForSectionIndexTitleAtIndex:index];
}</pre>
](javascript:void(0); "复制代码")
demo地址:https://github.com/WuKongCoo1/UILocalizedIndexedCollationDemo
标签: IOS
[好文要顶](javascript:void(0);) [关注我](javascript:void(0);) [收藏该文](javascript:void(0);) [ image ](javascript:void(0); "分享至新浪微博") [ image ](javascript:void(0); "分享至微信") image最新评论
- 1. Re:iOS 动画篇 (三) CADisplayLink与CoreGraphics实现动画
- +1
- --钻葛格
- 2. Re:iOS 动画篇(一) Core Animation
- 学习一下
- --自由布鲁斯
- 3. Re:利用CoreAnimation实现一个时间的进度条
- 就喜欢这种文章!
- --仁慈的父
- 4. Re:[iOS]手把手教你实现微信小视频
- @幻想无极不能。。...
- --pretty guy
- 5. Re:[iOS]手把手教你实现微信小视频
- @pretty guy就是当gif播放到一半的时候吧视频也播放到一半...
- --幻想无极
阅读排行榜
- 1. [iOS]手把手教你实现微信小视频(10128)
- 2. iOS 根据图片URL从本地相册获取图片(7974)
- 3. iOS 使用UILocalizedIndexedCollation实现区域索引标题(Section Indexed Title)即拼音排序(3795)
- 4. iOS:ABPeoplePickerNavigationController系统通讯录使用(3399)
- 5. XGPush集成(信鸽集成)demo(3382)
评论排行榜
- 1. [iOS]手把手教你实现微信小视频(15)
- 2. iOS:ABPeoplePickerNavigationController系统通讯录使用(6)
- 3. OC 解决NSArray、NSDictionary直接打印中文出现乱码的问题(4)
- 4. iOS:原生二维码扫描(2)
- 5. OC KVC总结(2)
推荐排行榜
- 1. [iOS]手把手教你实现微信小视频(8)
- 2. iOS:ABPeoplePickerNavigationController系统通讯录使用(4)
- 3. iOS:原生二维码扫描(3)
- 4. iOS 动画篇 (三) CADisplayLink与CoreGraphics实现动画(1)
- 5. iOS 动画篇(一) Core Animation(1)
Copyright ©2018 pretty guy