一个简单的类似通讯录的封装代码
2017-05-11 08:59
429 查看
废话不多说上代码 直接使用该方法就可以
- (void)allDataRanger { // 通讯录排序,分组 UILocalizedIndexedCollation *collation = [UILocalizedIndexedCollation currentCollation]; //得出collation索引的数量,这里是27个(26个字母和1个#) NSInteger sectionTitlesCount = [[collation sectionTitles] count]; 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下 _dataSource存储的是模型数组 for (WDCar *model in _dataSource) { //获取name属性的值所在的位置,比如"林丹",首字母是L,在A~Z中排第11(第一位是0),sectionNumber就为11 NSInteger sectionNumber = [collation sectionForObject:model collationStringSelector:@selector(name)]; //把name为“林丹”的p加入newSectionsArray中的第11个数组中去 NSMutableArray *sectionNames = newSectionsArray[sectionNumber]; [sectionNames addObject:model]; } //对每个section中的数组按照name属性排序 for (NSInteger index = 0; index < sectionTitlesCount; index++) { NSMutableArray *personArrayForSection = newSectionsArray[index]; NSArray *sortedPersonArrayForSection = [collation sortedArrayFromArray:pe abfa rsonArrayForSection collationStringSelector:@selector(name)]; newSectionsArray[index] = sortedPersonArrayForSection; } //将排序好的通讯录名单存到数据库里面 _indexDataSource存储的是计算出来的字母 // [messageDbManger saveUserToDataBase:_allDataSource]; NSMutableArray *temp = [NSMutableArray new]; _indexDataSource = [NSMutableArray new]; [newSectionsArray enumerateObjectsUsingBlock:^(NSArray *arr, NSUInteger idx, BOOL *stop) { if (arr.count == 0) { [temp addObject:arr]; } else { [_indexDataSource addObject:[collation sectionTitles][idx]]; } }]; // 存储的是每个字母下面的模型数组 _allDataSource =[[NSMutableArray alloc]init]; [newSectionsArray removeObjectsInArray:temp]; [_allDataSource removeAllObjects]; [_allDataSource addObjectsFromArray:newSectionsArray];
相关文章推荐
- 一个简单类似朋友圈头像背景的代码
- 分享实现类似QQ的自动登陆的方法,代码比较简单,主要是给大家提供一个实现逻辑,具体的要结合自身的app来做
- 200行python代码实现一个类似django的简单模板引擎
- 代码改变世界 | 如何封装一个简单的 Koa
- 今天根据别人的代码简单的封装了一个将java bean生成xml的一个工具类,运用了反射和泛型,重用性高。
- 简单封装一个AlertDialog,调用只需要两行代码
- Android简单封装类似JQuery异步请求代码
- 【iOS开发-50】利用创建新的类实现代码封装,从而不知不觉实践一个简单的MVC实验,附带个动画
- WebBrowser Control的一个简单C++封装
- 一个简单的猜谜游戏的代码(高级用户界面制作)
- 分形介绍 && 一个简单的Kotch curve实现代码
- 用VB6写的一个简单俄罗斯方块代码
- 用VB6写的一个简单俄罗斯方块代码
- 一个VC写的完整、简单的Sniffer代码
- 一个简单的动态编译器,支持C#和VB.NET。当你想测试一些简单的代码的时候可以使用。
- 封装一个简单的同步Ajax函数
- 一个简单封装用户命令的C++程序
- 一个简单的AJAX实现,基于C#的ASP.Net,包括服务器端的程序代码
- ReSharper——一个类似eclipse 的代码工具
- sharpziplib是一个不错的在线压缩软软件.用VB.net简单写了个使用代码!