addressBookHelper
来源:互联网 发布:陪我聊天软件 编辑:程序博客网 时间:2024/04/30 09:40
/** * Model类:存储数据的类叫做Model类.(AddressPerson,PhotoInfo) Model层的类:只要和数据有关的类都是Model层的类.(比如:数据存储的类,数据处理的类,数据请求类,数据解析类). *//** * MVC:M层的任务是为了分担Controller数据处理的功能,让C只处理M与V之间的通讯,不做数据处理.而AddressBookHelper就是帮助AddressBookListController瘦身的,分担数据数据的模块. 而对于该助手类来说,对于数据处理,只需要一个助手就可以了,所以该类采用单例设计模式. */@interface FYZAddressBookHelper : NSObject+ (FYZAddressBookHelper *)sharedAddressBookHelper;//返回分区的个数+ (NSInteger)numberOfSections;//返回指定分区的行数+ (NSInteger)numberOfRowsInSection:(NSInteger)section;//返回对应的AddressPerson对象+ (AddressPerson *)addressPersonAtIndexPath:(NSIndexPath *)indexPath;//返回每个分区的title+ (NSString *)titleForHeaderInSection:(NSInteger )section;//返回索引值+ (NSArray *)sectionIndexTitles;//是否删除整个分区+ (BOOL)isNeedDeleteSection:(NSInteger)section;//删除整个分区+ (void)deleteOneSection:(NSInteger)section;//删除一行+ (void)deleteRowAtIndexPath:(NSIndexPath *)indexPath;//移动数据+ (void)moveAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath;//添加数据+ (void)addAddresPerson:(AddressPerson *)person;//保存数据+ (void)saveData;@end<p style="margin-top: 0px; margin-bottom: 0px; font-size: 24px; font-family: Menlo; color: rgb(201, 27, 19);"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc">#import "FYZAddressBookHelper.h"#import "NSString+Addition.h"#define kFileName @"person.plist"@interface FYZAddressBookHelper ()@property (nonatomic, retain) NSMutableDictionary *addressBookDic; //存储联系人信息@property (nonatomic, retain) NSMutableArray *orderedKeys;//存储排好序的key@end@implementation FYZAddressBookHelper//把该静态变量定义在方法之外,在整个.m文件中都可以访问.static FYZAddressBookHelper *helper = nil;+ (FYZAddressBookHelper *)sharedAddressBookHelper{ //加一个同步锁,保证同一时刻只有一个线程访问. @synchronized(self) { if (!helper) { helper = [[FYZAddressBookHelper alloc] init]; //判断之前在Documents文件夹下是否存储过数据, //(1)先获取person.plist文件路径 NSString *filePath = [self getFilePath]; //(2)判断该路径是否存在。(路径是否正确) NSFileManager *manager = [NSFileManager defaultManager]; BOOL isExist = [manager fileExistsAtPath:filePath]; if (isExist) { //如果存储过数据,则在Documents文件夹下读取, [helper readDataFromDocuments]; } else { //如果没有,则在包中的plist文件读取. [helper readDataFromPlist]; } } } return helper;}//从documents文件夹下的person.plist文件中读取//反归档- (void)readDataFromDocuments{ //根基文件路径初始化一个NSMutableData对象 NSMutableData *data = [NSMutableData dataWithContentsOfFile:[[self class] getFilePath]]; //创建反归档对象 NSKeyedUnarchiver *unArchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data]; //反归档 self.addressBookDic = [unArchiver decodeObjectForKey:@"person"]; //结束反归档 [unArchiver finishDecoding]; RELEASE_SAFE(unArchiver); //获取排好序的key值 NSArray *sortedKeys = [[self.addressBookDic allKeys] sortedArrayUsingSelector:@selector(compare:)]; //将不可变的数组转化为可变数组 self.orderedKeys = [NSMutableArray arrayWithArray:sortedKeys];}//从本地文件读取数据- (void)readDataFromPlist{ //1.先获取文件路径 NSString *filePath = [[NSBundle mainBundle] pathForResource:@"AddressBook" ofType:@"plist"]; //2.根据文件路径初始化字典对象 NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:filePath]; //3.创建可变字典存储联系人数据 self.addressBookDic = [NSMutableDictionary dictionaryWithDictionary:dic]; //4.将字典封装成AddressPerson对象 //(1)遍历字典得到key for (NSString *key in dic) { //(2)根据key获取对应的数组 NSArray *group = dic[key]; //(3)创建可变数组,存储AddressPerson对象 NSMutableArray *newGroup = [NSMutableArray array]; //(4)遍历数组,得到字典,将字典封装成AddressPerson对象 for (NSDictionary *tempDic in group) { AddressPerson *person = [[AddressPerson alloc] initWithDic:tempDic]; [newGroup addObject:person]; RELEASE_SAFE(person); } //(5)替换掉字典中key对应的存储字典的不可变数组.(使用存储对象的可变数组) [self.addressBookDic setObject:newGroup forKey:key]; } //5.获取字典中所有的key,并且升序排序 NSArray *sortedKeys = [[self.addressBookDic allKeys] sortedArrayUsingSelector:@selector(compare:)]; //6.将不可变数组转换成可变数组 self.orderedKeys = [NSMutableArray arrayWithArray:sortedKeys];}+ (NSInteger)numberOfSections{ return [self sharedAddressBookHelper].orderedKeys.count;}+ (NSInteger)numberOfRowsInSection:(NSInteger)section{ //1.先获取key// NSString *key = helper.orderedKeys[section];// //2.根据key获取对应的小数组// NSMutableArray *group = helper.addressBookDic[key];// //3.求出小数组元素个数// return [group count]; return [helper.addressBookDic[helper.orderedKeys[section]] count];}+ (AddressPerson *)addressPersonAtIndexPath:(NSIndexPath *)indexPath{ //1.先获取key //2.获取key 对应的小数组 //3.从小数组中获取对应的对象 return helper.addressBookDic[helper.orderedKeys[indexPath.section]][indexPath.row];}+ (NSString *)titleForHeaderInSection:(NSInteger )section{ return helper.orderedKeys[section];}+ (NSArray *)sectionIndexTitles{ return helper.orderedKeys;}//是否删除整个分区+ (BOOL)isNeedDeleteSection:(NSInteger)section{ //当对应的数组中只有一个元素时,需要删除整个分区. NSMutableArray *group = helper.addressBookDic[helper.orderedKeys[section]]; return group.count == 1 ? YES : NO;}//删除整个分区+ (void)deleteOneSection:(NSInteger)section{ //(1)从字典中移除对应的分组 NSString *key = helper.orderedKeys[section]; [helper.addressBookDic removeObjectForKey:key]; //(2)从排好序的key中移除分组对应的key [helper.orderedKeys removeObjectAtIndex:section];}//删除一行+ (void)deleteRowAtIndexPath:(NSIndexPath *)indexPath{ //(1)先获取key NSString *key = helper.orderedKeys[indexPath.section]; //(2)获取数组 NSMutableArray *group = helper.addressBookDic[key]; //(3)移除元素 [group removeObjectAtIndex:indexPath.row];}//移动+ (void)moveAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{ //(1)将原来数据删除掉 NSString *key = helper.orderedKeys[sourceIndexPath.section]; NSMutableArray *group = helper.addressBookDic[key]; //在删除之前先retain AddressPerson *person = [[group objectAtIndex:sourceIndexPath.row] retain]; [group removeObjectAtIndex:sourceIndexPath.row]; //(2)插入新的数据 [group insertObject:person atIndex:destinationIndexPath.row]; RELEASE_SAFE(person);}//@"范玉贞" fanyuzhen //F+ (void)addAddresPerson:(AddressPerson *)person{ //获取拼音第一个字符,@"F" NSString *first = [person.name firstCharactor]; //(1)根据key从字典中取对应的分组 NSMutableArray *group = helper.addressBookDic[first];//@"F" if (!group) { //如果字典中没有该元素 group = [NSMutableArray array]; //将对应key与分组存入字典中 [helper.addressBookDic setObject:group forKey:first]; //添加联系人 //重新读取orderedKeys //5.获取字典中所有的key,并且升序排序 NSArray *sortedKeys = [[helper.addressBookDic allKeys] sortedArrayUsingSelector:@selector(compare:)]; //6.将不可变数组转换成可变数组 helper.orderedKeys = [NSMutableArray arrayWithArray:sortedKeys]; } [group addObject:person];}//归档+ (void)saveData{ //1.获取存储的内容 //helper.addressBookDic; //2.获取存储的文件路径 NSString *filePath = [self getFilePath]; NSMutableData *data = [NSMutableData data]; //创建归档对象 NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data]; //归档 [archiver encodeObject:helper.addressBookDic forKey:@"person"]; //结束归档 [archiver finishEncoding]; //写入文件 [data writeToFile:filePath atomically:YES]; RELEASE_SAFE(archiver);}+ (NSString *)getFilePath{ //(1)获取documents文件路径 NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]; //(2)获取存储的文件路径(拼接) NSString *filePath = [documentsPath stringByAppendingPathComponent:kFileName]; return filePath;}- (void)dealloc{ RELEASE_SAFE(_addressBookDic); RELEASE_SAFE(_orderedKeys); [super dealloc];}@end
0 0