IOS开发教程第一季之01UI基础day9合并IOS学习010单元格模板(应用管理)

1.APP应用管理

plist文件apps_full.plist
在这里插入图片描述
1、创建FRApp模型类
FRApp.h

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface FRApp : NSObject
@property(nonatomic,copy)NSString* size;//软件大小
@property(nonatomic,copy)NSString* download;//下载量
@property(nonatomic,copy)NSString* name;//软件名称
@property(nonatomic,copy)NSString* icon;//软件图片

//增加一个用来标记是否下载过(点击过)的一个属性
@property(nonatomic,assign)BOOL isDownload;

-(instancetype)initWithDictionary:(NSDictionary*)dict;
+(instancetype)appWithDictionary:(NSDictionary*)dict;

@end

NS_ASSUME_NONNULL_END

FRApp.m

#import "FRApp.h"

@implementation FRApp
-(instancetype)initWithDictionary:(NSDictionary*)dict{
  if (self= [super init]) {
    //按照字典内的键值对给成员属性赋值
    [self setValuesForKeysWithDictionary:dict];
  }
  return self;
}
+(instancetype)appWithDictionary:(NSDictionary*)dict{
  
  return [[self alloc]initWithDictionary:dict];
}
@end

2由于使用了单元格模板,因此要对模板单元格创建FRAppCell类
FRAppCell.h

#import <UIKit/UIKit.h>
#import "FRApp.h"
@class FRAppCell;
NS_ASSUME_NONNULL_BEGIN
@protocol FRAppCellDelegate <NSObject>

-(void)appCellDidClickDownloadButton:(FRAppCell*)appCell;

@end
@class FRApp;
@interface FRAppCell : UITableViewCell
//创建一个单元格中的数据模型
@property(nonatomic,strong)FRApp* app;
//创建一个代理属性,这个属性的id类型并且实现了FRAppCellDelagate协议的
@property(nonatomic,weak) id<FRAppCellDelegate>  delegate;
@end

NS_ASSUME_NONNULL_END

FRAppCell.m

#import "FRAppCell.h"
#import "FRApp.h"
@interface FRAppCell()
@property (weak, nonatomic) IBOutlet UIImageView *imgViewIcon;//软件图片
@property (weak, nonatomic) IBOutlet UILabel *lblName;//软件名称
@property (weak, nonatomic) IBOutlet UILabel *lblIntro;//软件大小
@property (weak, nonatomic) IBOutlet UIButton *btnDownload;//下载量

- (IBAction)btnDownloadClick;

@end
@implementation FRAppCell
//重写成员属性app的set方法
-(void)setApp:(FRApp *)app{
  //自己先赋值
  _app=app;
  //将获得的模型中的各个属性赋值给新创建的的
  self.imgViewIcon.image=[UIImage imageNamed:app.icon];
  self.lblName.text=app.name;
  self.lblIntro.text=[NSString stringWithFormat:@"大小:%@ | %@",app.size,app.download];
  if (app.isDownload) {
      self.btnDownload.enabled=NO;
  }else{
    self.btnDownload.enabled=YES;
  }
}
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
//按钮的点击事件
- (IBAction)btnDownloadClick {
  //禁用按钮
  self.btnDownload.enabled=NO;
  //设置标记已经被点击过
  self.app.isDownload=YES;
  
  //弹出消息提示按钮
  //使用协议,selector的方法为刚才在.h文件中创建的协议方法,表示要求代理对象来执行这个方法
  if ([self.delegate respondsToSelector:@selector(appCellDidClickDownloadButton:)]) {
    //如果appCellDidClickDownloadButton方法存在,则调用这个方法
    [self.delegate appCellDidClickDownloadButton:self];

  }
  
}
@end

3整个使用tableViewController作为控制器,删除原有ViewController,创建FRAppViewController类

FRAppViewController.m

#import "FRAppViewController.h"
#import "FRApp.h"
#import "FRAppCell.h"
@interface FRAppViewController ()<FRAppCellDelegate>

//懒加载数据必须有一个用来存放模型的数组
@property(nonatomic,strong)NSArray* apps;
@end

@implementation FRAppViewController
#pragma mark *******实现代理方法******
-(void)appCellDidClickDownloadButton:(FRAppCell*)appCell{

  //创建一个Label
  UILabel* lblMsg=[[UILabel alloc]init];
  lblMsg.text=@"正在下载……";
  lblMsg.backgroundColor=[UIColor blackColor];
  lblMsg.textColor=[UIColor redColor];

  lblMsg.font=[UIFont systemFontOfSize:13];
  CGFloat msgW=200;
  CGFloat msgH=100;
  CGFloat msgX=(self.view.frame.size.width-msgW)*0.5;
  CGFloat msgY=(self.view.frame.size.height-msgH)*0.5;
  
  lblMsg.frame=CGRectMake(msgX, msgY, msgW, msgH);
  
  //修改文字大小
  lblMsg.font=[UIFont systemFontOfSize:17];
  lblMsg.textAlignment=NSTextAlignmentCenter;
  lblMsg.alpha=0.0;
  lblMsg.layer.cornerRadius=10;
  lblMsg.layer.masksToBounds=YES;
  //把label加载到selfview里
  //[self.view addSubview:lblMsg];//这里的self.view是tableView,会导致弹出框跟随tableview滚动
  //以下的keyWindow这个方法在ios13中被启用了,在iphone11中可以正常使用
  [[[UIApplication sharedApplication]keyWindow]addSubview:lblMsg];
  
  //设定以动画的方式展现对话框
  [UIView animateWithDuration:1.0 animations:^{
    //1秒钟时间动画变动到0.6透明度
    lblMsg.alpha=0.6;
    
  } completion:^(BOOL finished) {
    //当动画执行完毕以后执行这里的代码
    //再开启一个新的动画
    [UIView animateWithDuration:1.0 delay:1.5 options:UIViewAnimationOptionCurveLinear animations:^{
      //延迟0.5秒钟后,用1秒钟的时间让对话框消失(alpha回到0)
      lblMsg.alpha=0.0;

    } completion:^(BOOL finished) {
      //对话框消失后,将对话框中父控件中移除
      [lblMsg removeFromSuperview];
    }];
    
  }];
  
}


#pragma mark *******懒加载数据********
//重写这个模型数组的get方法
-(NSArray*)apps{
  if (_apps==nil) {
    //创建路径字符串
    NSString* path=[[NSBundle mainBundle]pathForResource:@"apps_full.plist" ofType:nil];
    //通过路径字符串或缺的字典数组
    NSArray* arryDicts=[NSArray arrayWithContentsOfFile:path];
    
    //创建可变模型数组
    NSMutableArray* arraymodels=[NSMutableArray array];
    
    //遍历字典数组并将其加载到模型数组中
    for (NSDictionary* dict in arryDicts) {
      //创建模型
      FRApp* model=[FRApp appWithDictionary:dict];
      [arraymodels addObject:model];
    }
    _apps=arraymodels;
  }
  //返回模型数组
  return _apps;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
  self.tableView.rowHeight=60;
}

#pragma mark *******数据源方法********
//返回组数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
//返回行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  return self.apps.count;
}

//返回单元格
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  //获得模型
  FRApp* model=self.apps[indexPath.row];
  
  //创建单元格
  static NSString* cellId=@"app_cell";
  //因为有模板,实际上不需要再判断if cell是否存在并创建了,如果cell不存在,会根据模板样式直接创建一个cell
  FRAppCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
  
  //设置单元格的代理
  cell.delegate=self;
  
  //把模型数据设置个单元格
  cell.app=model;

  //返回单元格
  return cell;
}


@end

效果:

在这里插入图片描述

2.关于KVC(Key Value Coding)

创建两个类,各有其属性
person.h

#import <Foundation/Foundation.h>
#import "Dog.h"
NS_ASSUME_NONNULL_BEGIN

@interface Person : NSObject
@property(nonatomic,copy)NSString* name;
@property(nonatomic,strong)Dog* dog;
@property(nonatomic,assign)int age;
@property(nonatomic,copy)NSString* email;
@end

NS_ASSUME_NONNULL_END

dog.h

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface Dog : NSObject
@property(nonatomic,copy)NSString* name;
@end

NS_ASSUME_NONNULL_END

main方法
main.m
#import <Foundation/Foundation.h>
#import “Dog.h”
#import “Person.h”
int main(int argc, const char * argv[]) {
@autoreleasepool {
//直接为对象的属性赋值
Person* p1=[[Person alloc]init];
p1.name=@“张三”;

Dog *jiwawa=[[Dog alloc]init];
jiwawa.name=@"吉娃娃";
p1.dog=jiwawa;
NSLog(@"%@----%@",p1.name,p1.dog.name);

//使用kvc来赋值
Dog *hashiqi=[[Dog alloc]init];
hashiqi.name=@"哈士奇";


[p1 setValue:@"李四" forKeyPath:@"name"];//这里name是成员属性,setvalue后面是属性值
[p1 setValue:@10 forKeyPath:@"age"];
[p1 setValue:hashiqi forKeyPath:@"dog"];
NSLog(@"%@----%d---%@",p1.name,p1.age,p1.dog.name);

NSString* propertyName=@"name";
[p1 setValue:@"王五" forKeyPath:propertyName];
NSLog(@"人的名字叫%@",p1.name);
propertyName=@"age";
[p1 setValue:@50 forKeyPath:propertyName];
NSLog(@"%d",p1.age);

NSString* value=@"赵六";
NSString* property=@"name";
[p1 setValue:value forKeyPath:property];
NSLog(@"%@",p1.name);

value=@"孙七";
property=@"name";
[p1 setValue:value forKeyPath:property];//这种方式的赋值非常灵活
NSLog(@"%@",p1.name);

//通过字典直接为对象赋值(实际是批量赋值)
Person* p2=[[Person alloc]init];
NSDictionary* dict=@{
  @"name":@"小凤九",
  @"age":@28,
  @"email":@"hello@126.com",
  @"dog":@{@"name":@"中华田园"}
};
[p2 setValuesForKeysWithDictionary:dict];
NSDictionary* dogdict=(NSDictionary*)p2.dog;//这里p2是个字典,取出来以后需要强转为字典
NSLog(@"%@----%d---%@---%@",p2.name,p2.age,p2.email,dogdict[@"name"]);

//把对象转成字典
Person* p3=[[Person alloc]init];
Dog* mydog=[[Dog alloc]init];
[p3 setValue:@"王十" forKeyPath:@"name"];
[p3 setValue:@"wangshi@126.com" forKeyPath:@"email"];
[p3 setValue:@32 forKeyPath:@"age"];
[p3 setValue:mydog forKeyPath:@"dog"];//初始化出一个dog对象出来
[p3 setValue:@"大金毛" forKeyPath:@"dog.name"];//这个dog.name体现出来了path的路径的意义
NSLog(@"%@----%d---%@---%@",p3.name,p3.age,p3.email,p3.dog.name);

NSString* name=[p3 valueForKeyPath:@"name"];
NSArray* dogname=[p3 valueForKeyPath:@"dog.name"];
NSLog(@"%@--%@",name,dogname);
//把对象转成字典
NSArray* keys=@[@"name",@"age",@"email",@"dog"];//创建一个包含对象属性名的字典
NSDictionary* newDict=[p3 dictionaryWithValuesForKeys:keys];
NSLog(@"%@",newDict);
NSLog(@"%@",[newDict[@"dog"] name]);

}
return 0;
}
结果:
2020-07-10 21:48:26.101006+0800 Mac003[2099:142105] 张三----吉娃娃
2020-07-10 21:48:26.101495+0800 Mac003[2099:142105] 李四----10—哈士奇
2020-07-10 21:48:26.101559+0800 Mac003[2099:142105] 人的名字叫王五
2020-07-10 21:48:26.101588+0800 Mac003[2099:142105] 50
2020-07-10 21:48:26.101611+0800 Mac003[2099:142105] 赵六
2020-07-10 21:48:26.101631+0800 Mac003[2099:142105] 孙七
2020-07-10 21:48:26.101674+0800 Mac003[2099:142105] 小凤九----28—hello@126.com—中华田园
2020-07-10 21:48:26.101730+0800 Mac003[2099:142105] 王十----32—wangshi@126.com—大金毛
2020-07-10 21:48:26.101766+0800 Mac003[2099:142105] 王十–大金毛
2020-07-10 21:48:26.101964+0800 Mac003[2099:142105] {
age = 32;
dog = “<Dog: 0x101cb5750>”;
email = “wangshi@126.com”;
name = “\U738b\U5341”;
}
2020-07-10 21:48:26.102001+0800 Mac003[2099:142105] 大金毛
Program ended with exit code: 0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值