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];//单独定制当去前界面的导航条的颜色,titleself.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;
@endUIImage+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;
@endPerson.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

转载于:https://www.cnblogs.com/wohaoxue/p/4805652.html

iOS中实现plist中读取数据实现Cell的显示(字典转模型,实现按序分组)修改图片的尺寸...相关推荐

  1. python如何读取数据时出现错误_在python3中,关于redis读取数据带有‘b’的问题...

    在python3中,关于redis读取数据带有'b'的问题 #encoding=utf-8 from redis import * #读取数据 d1=input("您输入的数据是:" ...

  2. python中如何打开csv文件_在Python中从CSV文件读取数据

    我正在从包含以下数据的CSV文件(xyz.CSV)中读取数据: col1,col2,col3,col4 name1,empId1,241682-27638-USD-CIGGNT ,1 name2,em ...

  3. 从网络读取数据并动态的显示在ListView中

    这两天写了个小程序,使用了从网络读取xml数据,并显示在ListView中. 这里面有几个关键点: 从网络读取数据 SAX解析xml 异步填充ListView 先看下截图: 非常简单的界面哈 为了方便 ...

  4. Android中,Sqlite数据库读取数据为空的问题

    公司使用的触摸屏出现了一个奇怪的问题,在app中读取数据,只是简单的select * from table 也查询不到数据. [问题描述] 后来发现是有两个module单例分别连接数据库,一个modu ...

  5. python从csv中读取数据打印到屏幕上-从Python中的CSV文件读取数据

    我正在从包含以下数据的CSV文件(xyz.CSV)读取数据:col1,col2,col3,col4 name1,empId1,241682-27638-USD-CIGGNT ,1 name2,empI ...

  6. qt 从文件中读出数据显示在表格中_java如何从文件中读取数据存在数组再显示在表格中!!...

    再谢谢你昨天帮我解决了那个读数据再显示在JTextArea中,现在我想显示在表格中啦!而且对应显示!数据一行一行读,再存在arraylist中数据:何子杨13580130150652150计算机丁香维 ...

  7. mfc从mysql中读取数据类型_在MFC中使用SQlite数据库读取数据

    本人在数据库里面用回调函数来处理读取函数的返回值,回调函数必须使用static类型才可以,这样处理起返回的数据变得非常的麻烦,很难处理结果集. 后来从网上找了一个预编译的例子,放在网上和网友分享. 例 ...

  8. opencv 中CV_32F和CV_64读取数据结果不一致

    我需要读取matlab生成的txt,里面存放着3*3的矩阵,我在尝试用opencv读取txt 并转存为yaml文件时出现了问题. 原始数据 3.2212523e-01 -3.2059794e-02 - ...

  9. Reveal分析IOS界面,plist文件读取

    Reveal分析IOS界面,需要得到app的 softwareVersionBundleId上传到iphone中 , 而IOS8的iTunesMetadata.plist (设备路径/var/mobi ...

  10. Echarts中折线图Y轴数据值太长显示不全-解决办法

    正常状态 不全的情况 所有的数据都是从后台取的,也就是说动态变化的,一开始的时候数据量不大不会出现问题,后面y轴的值越来越大的时候就出现了这个显示不全的情况 代码 <div id="c ...

最新文章

  1. 【php】php5.0以上,instanceof 用法
  2. 机器学习笔记:梯度下降
  3. 计算机主机内部结构连接,电脑主机内部结构介绍
  4. 计算机指令系统课件,计算机组成原理课件05指令系统.ppt
  5. Python3 函数注释: 参数 中 的 冒号 与 箭头
  6. linux 安装log4j,Log4j 安装
  7. 手机html端悬浮球,手机移动端网站触屏可拖动悬浮球
  8. 索尼斥资11.7亿美元收购AT&T动漫业务Crunchyroll
  9. 已知若干点求圆心_【求精干货】高中数学知识点总结归纳高一学生必须掌握
  10. [DJANGO] excel十几万行数据快速导入数据库研究
  11. FastDFS下的storage服务启动卡住
  12. 本科计算机技术与应用知识点,《软件工程》经典考试例题题,重点的知识点(含答案)...
  13. Navicat Win 和 Mac 视图类快捷键对比
  14. mysql在线修复主从同步
  15. “VMRC控制台的连接已断开…正在尝试重新连接”的解决方法
  16. Hyperscan中的 NFA模型演化
  17. GeoTrans2.4.1 用户手册 之 入门
  18. PWM如何控制直流电机
  19. uni-app自定义全屏切换组件
  20. 准备嵌入式Linux开发环境

热门文章

  1. 系统类配置(三)【ubuntu14.04或者ubuntu16.04 配置caffe】
  2. IP通信基础 3月6日
  3. 跨库访问-dblink
  4. NHibernate教程(14)--使用视图
  5. JavaScript对象Object
  6. 【jsp】JSTL标签大全详解
  7. 【转】VC2008 配置OpenGL环境
  8. Delphi 调试连接 任意Android手机/平板/盒子
  9. ZZFAFA_BilibiliMusic_DownUrl
  10. NodeJS写模块和引入模块的例子