iOS UILocalizedIndexedCollation排序神器
来源:互联网 发布:免费聊天软件 编辑:程序博客网 时间:2024/05/01 00:47
UILocalizedIndexedCollation 是一个帮助我们组织列表数据的类,它能够根据地区来生成与之对应区域索引标题。不需要直接创建它的对象,我们可以通过 UILocalizedIndexedCollation +currentCollation 获得一个对应当前地区的单例对象。
项目地址:https://github.com/MisterZhouZhou/UISearchControllerAndUILocalizedCollection
简单操作如下:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return [[[UILocalizedIndexedCollation currentCollation] sectionTitles]count];}-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return 2;}-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *ID = @"cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; if (!cell) { cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID]; } cell.textLabel.text = @"cell"; return cell;}- (NSString *)tableView:(UITableView *)tableViewtitleForHeaderInSection:(NSInteger)section{ return [[[UILocalizedIndexedCollation currentCollation] sectionTitles] objectAtIndex:section];}- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView { return [[UILocalizedIndexedCollation currentCollation] sectionIndexTitles];}- (NSInteger)tableView:(UITableView *)tableViewsectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{ return [[UILocalizedIndexedCollation currentCollation] sectionForSectionIndexTitleAtIndex:index];}
效果图:
下面开始对汉姓进行排序:
1、初始化数据:
//配置数据源 NSArray *firstNameArray = @[@"赵",@"钱",@"孙",@"李",@"周",@"吴",@"郑",@"王",@"郭",@"松",@"宋",@"长",@"大",@"小"]; NSMutableArray *tempArray = [NSMutableArray array]; for (int i=0; i<firstNameArray.count; i++) { Person *p = [Person new]; p.name = [NSString stringWithFormat:@"%@",firstNameArray[i]]; [tempArray addObject:p]; }
2、对数据进行排序筛选
//初始化UILocalizedIndexedCollation localizedCollection = [UILocalizedIndexedCollation currentCollation]; //得出collation索引的数量,这里是27个(26个字母和1个#) NSInteger sectionTitlesCount = [[localizedCollection sectionTitles] count]; //初始化一个数组newSectionsArray用来存放最终的数据 NSMutableArray *newSectionsArray = [[NSMutableArray alloc] initWithCapacity:sectionTitlesCount]; //初始化27个空数组加入newSectionsArray for (NSInteger index = 0; index < sectionTitlesCount; index++) { NSMutableArray *array = [[NSMutableArray alloc] init]; [newSectionsArray addObject:array]; } //将每个人按name分到某个section下 for (Person *temp in tempArray) { //获取name属性的值所在的位置,比如"林丹",首字母是L,在A~Z中排第11(第一位是0),sectionNumber就为11 NSInteger sectionNumber = [localizedCollection sectionForObject:temp collationStringSelector:@selector(name)]; NSMutableArray *sectionNames = newSectionsArray[sectionNumber]; [sectionNames addObject:temp]; } //对每个section中的数组按照name属性排序 for (int index = 0; index < sectionTitlesCount; index++) { NSMutableArray *personArrayForSection = newSectionsArray[index]; NSArray *sortedPersonArrayForSection = [localizedCollection sortedArrayFromArray:personArrayForSection collationStringSelector:@selector(name)]; newSectionsArray[index] = sortedPersonArrayForSection; } //section title sectionTitleArray = [NSMutableArray array]; NSMutableArray *tempArr = [NSMutableArray array]; [newSectionsArray enumerateObjectsUsingBlock:^(NSArray *array, NSUInteger idx, BOOL * _Nonnull stop) { if (array.count == 0) { [tempArr addObject:array]; }else{ [sectionTitleArray addObject:[localizedCollection sectionTitles][idx]]; } }]; [newSectionsArray removeObjectsInArray:tempArr]; dataArray = newSectionsArray.copy;
效果图:
0 0
- iOS UILocalizedIndexedCollation排序神器
- iOS开发------通讯录分组排序(UILocalizedIndexedCollation)
- iOS UILocalizedIndexedCollation
- 本地化下按首字母分组排序的神器——UILocalizedIndexedCollation
- 本地化下按首字母分组排序的神器——UILocalizedIndexedCollation
- 本地化下按首字母分组排序的神器——UILocalizedIndexedCollation
- 本地化下按首字母分组排序的神器——UILocalizedIndexedCollation
- 本地化下按首字母分组排序的神器——UILocalizedIndexedCollation
- 本地化下按首字母分组排序的神器——UILocalizedIndexedCollation
- 通讯录分组排序(UILocalizedIndexedCollation)
- UILocalizedIndexedCollation ios首字母排列
- UILocalizedIndexedCollation
- IOS开发(52)之UITableView索引搜索之UILocalizedIndexedCollation
- iOS 使用UILocalizedIndexedCollation实现通讯录样式的索引
- UILocalizedIndexedCollation 使用
- 13.UILocalizedIndexedCollation
- UILocalizedIndexedCollation简介
- js数组排序神器
- 新建Eclipse工作空间,复制原有的配置(转)
- 企业移动信息化解决方案:移动报销
- java集合架构(1)
- Alsa音频应用编程
- 常用排序算法之JavaScript实现
- iOS UILocalizedIndexedCollation排序神器
- phpize是什么
- 牛客网系列--java
- 机器学习入门
- 新机PE环境安装系统出现的问题,EFI RART发现红× 解决方法
- Sublime Text 3在Windows下配置C++环境
- ListView的侧滑删除原来如此简单
- idea启动tomcat远程debug
- Qt修炼手册9_Ui名字空间及setupUi()原理解读