RootViewController.m
#import "RootViewController.h" #import "UIImage+UIImageScale.h" @interface RootViewController ()<UITableViewDataSource,UITableViewDelegate> @property (nonatomic, retain) NSArray *apps; //存放排好序的keys @property (nonatomic, retain) NSMutableArray *keysArray; //字典转模型 @property (nonatomic, retain) NSMutableDictionary *addressdic; @end @implementation RootViewController //重写loadView ,将tableView指定为当前视图控制器的视图 - (void)loadView{ //1.创建UItableView类型的对象 UITableView *tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain]; //2.指定为视图控制器的根视图 self.view = tableView; //设置数据源 tableView.dataSource = self; //指定为视图的交互 tableView.delegate = self; //3.释放 [tableView release]; } - (void)viewDidLoad { [super viewDidLoad]; //获取初始化数据 [self readdataFormPlist]; //配置导航条 [self configureNavigationBarContent]; } #pragma mark -- Editing UITableViewDataSourse //设置tableView的那些行可以允许编辑 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{ return indexPath.section < 3 ? YES : NO; } #pragma mark - Edit UITableViewDelegate //设置tableView行的编辑样式(插入/删除) - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{ return indexPath.section < 2 ? UITableViewCellEditingStyleDelete : UITableViewCellEditingStyleInsert; } //提交编辑操作时,处理编辑操作 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{ //1.获取key值 NSString *key = self.keysArray[indexPath.section]; //获取删除行所在的数组 NSMutableArray *group = self.addressdic[key]; if (editingStyle == UITableViewCellEditingStyleDelete) { //处理删除操作 //判断是否需要删除一个分区(如果该分区只有一行,就删除整个分区,否则只删除一行) if (1 == group.count) { //删除整个分区 //1.修改数据源 //1.从字典中移除元素 [self.addressdic removeObjectForKey:key]; //2.从排好序的可以key值数组中移除对应的key [self.keysArray removeObject:key]; //2.修改界面 NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:indexPath.section]; [tableView deleteSections:indexSet withRowAnimation:UITableViewRowAnimationRight]; }else{ //删除一行 //1.修改数据源 [group removeObjectAtIndex:indexPath.row]; //2.修改界面 [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight]; } }else{ //处理添加操作 NSDictionary *dic = @{@"name":@"马云",@"gender":@"男",@"age":@"16",@"imageName":@"XDLG.png",@"motto":@"-群**成就一个男人",@"phoneNumber":@"123442342"}; //1.修改数据源 [group insertObject:dic atIndex:indexPath.row]; //2.修改界面 [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight]; } } #pragma mark --实现delegate的代理 UITableViewDelegate - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ NSLog(@"触发了"); } //设置删除时确认按钮的文字 - (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{ return @"删除"; } #pragma mark - moving UITableViewdataSource //设置tableView哪些行可以移动 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{ return indexPath.section <= 2 ? YES : NO; } //提交移动操作 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{ //移动操作,只需要,修改数据源即可 //1.获取行所在的数组 NSMutableArray *groupArray = self.addressdic[self.keysArray[sourceIndexPath.section]]; //2.获取对应行的数据,并且保存(要移动的那个人) //retain 是经典 当一个对象从集合中移除的时候 它的引用计数会减1 这些是集合自己管理的 ,retain 后当移除时 对象就不会被销毁. NSDictionary *dic = [groupArray[sourceIndexPath.row] retain]; //3.从数组中原来的位置移除对应的元素 [groupArray removeObjectAtIndex:sourceIndexPath.row]; //4.将元素插入到新的位值中 [groupArray insertObject:dic atIndex:destinationIndexPath.row]; [dic release]; } #pragma mark - moving UITavleViewDelegate //当停止拖动的时候,可以实现阻止cell的跨区移动 - (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath{ //sourceIndexPath cell移动之前的位置 //proposedDestinationIndexPath cell 想要移动到的位置 if (sourceIndexPath.section == proposedDestinationIndexPath.section) { return proposedDestinationIndexPath; }else{ return sourceIndexPath; } } #pragma mark --实现数据源中的方法 //组 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return self.addressdic.count; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ //在其他方法中需要的的到数据中的图片和个数,所以要把数据源存在属性中 NSString *key = self.keysArray[section]; NSArray *persons = [self.addressdic valueForKey:key]; return persons.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *ID = @"per"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ID] autorelease]; } NSString *key = self.keysArray[indexPath.section]; NSArray *persons = [self.addressdic valueForKey:key]; NSDictionary *dic = persons[indexPath.row]; NSString *strimage = dic[@"imageName"]; NSString *strText = dic[@"name"]; cell.imageView.image = [[UIImage imageNamed:strimage] scaleImageToSize:CGSizeMake(35, 40)]; cell.textLabel.text = strText; cell.detailTextLabel.text = dic[@"phoneNumber"]; return cell; } //设置分区title - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ return self.keysArray[section]; } //设置分区索引 - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{ return self.keysArray; } //初始化数据 - (void)readdataFormPlist { //1.获取本地文件 NSString *filePath = [[NSBundle mainBundle] pathForResource:@"ContactBook.plist" ofType:nil]; //2.初始化字典对象,最外层的是个字典对象 self.addressdic = [NSMutableDictionary dictionaryWithContentsOfFile:filePath]; //当对结合进行快速遍历forin时,不能对集合进行修改 //给字拷贝一个副本,对副本遍历,修改原版 NSMutableDictionary *tempDic = [NSMutableDictionary dictionaryWithDictionary:self.addressdic]; //将字典中的key对应得数组,由不可变数组变成可变可变数组 for (NSString *key in tempDic) { //根据key值取到不可变数组 NSArray *groupArray = self.addressdic[key]; //就不可变数组,改为可变数组 NSMutableArray *newMArray = [NSMutableArray arrayWithArray:groupArray]; //对字典添加 [self.addressdic setObject:newMArray forKey:key]; } //获取所有的key值 NSArray *arraykeys = [self.addressdic allKeys]; NSArray *sortedKeys = [arraykeys sortedArrayUsingSelector:@selector(compare:)]; self.keysArray = [NSMutableArray arrayWithArray:sortedKeys]; } //配置导航条 - (void)configureNavigationBarContent{ //修改导航条颜色 self.navigationController.navigationBar.barTintColor = [UIColor orangeColor]; //单独定制当去前界面的导航条的颜色,title self.navigationItem.title = @"河南32班通讯录"; //修改字体 NSDictionary *dic = @{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:[UIFont systemFontOfSize:20]}; self.navigationController.navigationBar.titleTextAttributes = dic; //在导航条右边添加Edit编辑按钮 self.navigationItem.rightBarButtonItem = self.editButtonItem; } //点击Edit按钮触发的方法 - (void)setEditing:(BOOL)editing animated:(BOOL)animated{ [super setEditing:editing animated:animated]; //让tableView进入编辑状体 [(UITableView *)self.view setEditing:editing animated:YES]; } - (void)dealloc { self.apps = nil; self.addressdic = nil; self.keysArray = nil; [super dealloc]; } @end
UIImage+UIImageScale.h #import <UIKit/UIKit.h> @interface UIImage (UIImageScale) - (UIImage *)scaleImageToSize:(CGSize) size; @end UIImage+UIImageScale.m #import "UIImage+UIImageScale.h" @implementation UIImage (UIImageScale) - (UIImage *)scaleImageToSize:(CGSize) size { //创建一个bitmap的context //并把它设置为当前正在使用的cntext UIGraphicsBeginImageContext(size); //绘制改的大小的图片 [self drawInRect:CGRectMake(0, 0, size.width, size.height)]; //从当前context中创建一个改变大小的图片 UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); //使用当前的context出堆栈 UIGraphicsEndImageContext(); //返回新的图片 return newImage; } @end
Person.h (模型) #import <Foundation/Foundation.h> @interface Person : NSObject @property (nonatomic, copy) NSString *imageName; @property (nonatomic, copy) NSString *name; @property (nonatomic, copy) NSString *gender; @property (nonatomic, copy) NSString *age; @property (nonatomic, copy) NSString *phoneNumber; @property (nonatomic, copy) NSString *motto; - (instancetype)initWithDic:(NSDictionary *)dic; + (instancetype)personWithDic:(NSDictionary *)dic; @end Person.m #import "Person.h" @implementation Person - (instancetype)initWithDic:(NSDictionary *)dic{ if (self = [super init]) { [self setValuesForKeysWithDictionary:dic]; } return self; } + (instancetype)personWithDic:(NSDictionary *)dic{ return [[self alloc] initWithDic:dic]; } @end
iOS中实现plist中读取数据实现Cell的显示(字典转模型,实现按序分组)修改图片的尺寸
原文:http://www.cnblogs.com/wohaoxue/p/4805652.html