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

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

 

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

CSDN海神之光上传的代码均可运行,亲测可用,直接替换数据即可,适合小白; 1、代码压缩包内容 主函数:main.m; 调用函数:其他m文件;无需运行 运行结果效果图; 2、代码运行版本 Matlab 2019b或2023b;若运行有误,根据提示修改;若不会,私信博主; 3、运行操作步骤 步骤一:将所有文件放到Matlab的当前文件夹; 步骤二:双击打开main.m文件; 步骤三:点击运行,等程序运行完得到结果; 4、仿真咨询 如需其他服务,可私信博主或扫描博客文章底部QQ名片; 4.1 博客或资源的完整代码提供 4.2 期刊或参考文献复现 4.3 Matlab程序定制 4.4 科研合作 功率谱估计: 故障诊断分析: 雷达通信:雷达LFM、MIMO、成像、定位、干扰、检测、信号分析、脉冲压缩 滤波估计:SOC估计 目标定位:WSN定位、滤波跟踪、目标定位 生物电信号:肌电信号EMG、脑电信号EEG、心电信号ECG 通信系统:DOA估计、编码译码、变分模态分解、管道泄漏、滤波器、数字信号处理+传输+分析+去噪(CEEMDAN)、数字信号调制、误码率、信号估计、DTMF、信号检测识别融合、LEACH协议、信号检测、水声通信 1. EMD(经验模态分解,Empirical Mode Decomposition) 2. TVF-EMD(时变滤波的经验模态分解,Time-Varying Filtered Empirical Mode Decomposition) 3. EEMD(集成经验模态分解,Ensemble Empirical Mode Decomposition) 4. VMD(变分模态分解,Variational Mode Decomposition) 5. CEEMDAN(完全自适应噪声集合经验模态分解,Complementary Ensemble Empirical Mode Decomposition with Adaptive Noise) 6. LMD(局部均值分解,Local Mean Decomposition) 7. RLMD(鲁棒局部均值分解, Robust Local Mean Decomposition) 8. ITD(固有时间尺度分解,Intrinsic Time Decomposition) 9. SVMD(逐次变分模态分解,Sequential Variational Mode Decomposition) 10. ICEEMDAN(改进的完全自适应噪声集合经验模态分解,Improved Complementary Ensemble Empirical Mode Decomposition with Adaptive Noise) 11. FMD(特征模式分解,Feature Mode Decomposition) 12. REMD(鲁棒经验模态分解,Robust Empirical Mode Decomposition) 13. SGMD(辛几何模态分解,Spectral-Grouping-based Mode Decomposition) 14. RLMD(鲁棒局部均值分解,Robust Intrinsic Time Decomposition) 15. ESMD(极点对称模态分解, extreme-point symmetric mode decomposition) 16. CEEMD(互补集合经验模态分解,Complementary Ensemble Empirical Mode Decomposition) 17. SSA(奇异谱分析,Singular Spectrum Analysis) 18. SWD(群分解,Swarm Decomposition) 19. RPSEMD(再生相移正弦辅助经验模态分解,Regenerated Phase-shifted Sinusoids assisted Empirical Mode Decomposition) 20. EWT(经验小波变换,Empirical Wavelet Transform) 21. DWT(离散小波变换,Discraete wavelet transform) 22. TDD(时域分解,Time Domain Decomposition) 23. MODWT(最大重叠离散小波变换,Maximal Overlap Discrete Wavelet Transform) 24. MEMD(多元经验模态分解,Multivariate Empirical Mode Decomposition) 25. MVMD(多元变分模态分解,Multivariate Variational Mode Decomposition)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值