iOS UI基础学习 Note_dayFour

今天的知识点不多,在第三天的基础上加载数据,懒加载,如何将字典转换为模型,学习自定义控件,还有构造方法的复习等等,今天学得比较吃力,对于一个初学者来说稍微有点儿复杂

加载数据

加载数据的方式有多种,比如用数组..if..switch..字典..数组+字典等.
我这里只介绍最后一种,将数组和字典写成一个.plist文件,例如

NSArray *arr = @[
                    @{@"name":@"zhangsan",@"age":@"20"},
                    @{@"name":@"lisi",@"age":@"18"},
                ]
[arr writeToFile:<#(nonnull NSString *)#> atomically:<#(BOOL)#>];
//writeToFile 填写文件生成路径,atomically YES:写入不成功不会生成文件 NO:写入失败时也会生成文件

将生成的.plist文件加载到Supporting Files中,这个时候我们就可以使用它了,下图是完善购物车demo的.plist文件
dayFour01

接下来是将plist文件中的数据取出(将字典转换为模型)

@property (nonatomic, strong) NSArray *dataArr;
//这里要注意 这是重写getter方法
- (NSArray *)dataArr{
//if语句是让数据只加载一次
    if (_dataArr == nil) {
        NSString *path = [[NSBundle mainBundle]pathForResource:@"dataArr.plist" ofType:nil];
        self.dataArr = [NSArray arrayWithContentsOfFile:path];
//赋值给全局变量的数据数组
        NSMutableArray *dataArrM = [NSMutableArray array];
        for (NSDictionary *dict in _dataArr ) {
            Shopdata *shop = [Shopdata ShopdataWithDict:dict];
//将遍历出来的数据赋值给一个接受数据模型的类Shopdata
            [dataArrM addObject:shop];
//又将数据赋值一遍给可变数组
        }
        self.dataArr = dataArrM;
//最后再一次赋值一次给全局变量数组
    }
    return _dataArr;
}

以上的代码其实并不需要放在viewDidLoad方法里面加载一次,因为这样如果viewDidLoad方法里面加载,会延长app启动的初始化时间加长,所以这个时候我们需要用到懒加载,只要用到它的时候再加载
所以我们把它放在了与添加键关联的初始化商品控件的方法里面加载

    ShopView *shopview = [[ShopView alloc]initWithShopdata:self.dataArr[index]];

因为最后的赋值是self.dataArr 所以调用了getter/setter方法 这个时候,上面强调为什么是重写getter方法
上面的代码还用到了构造方法(复习)和字典转换模型方法

  • 字典转换模型是将plist文件中的数据用一个类来接收和转换,这样做的好处是避免写过多的字典的Key,可以减少代码出错率
@interface Shopdata : NSObject

@property (nonatomic,copy) NSString * name;
//创建一个全局变量来接收plist文件中的name
@property (nonatomic,copy) NSString *icon;
//创建一个全局变量来接收plist文件中的icon

//构造方法 
- (instancetype)initWithDict:(NSDictionary *)dict;
+ (instancetype)ShopdataWithDict:(NSDictionary *)dict;
@end

@implementation Shopdata

- (instancetype)initWithDict:(NSDictionary *)dict
{
    if (self = [super init]) {
       self.icon = dict[@"icon"];
        self.name = dict[@"name"];
    }
    return self;
/*
将接受到的字典赋值给数据类用类,并赋值给数据类的全局变量
返回数据
*/
}

+ (instancetype)ShopdataWithDict:(NSDictionary *)dict
{
    return [[self alloc]initWithDict:dict];
//将接收到的数据封装成一个方法
}
@end

自定义控件

demo中的商品包含一个UIImageView和一个UILabel 所以自定义一个控件包含他们,这个时候创建一个类,继承与UIView,代码如下

@property (nonatomic, strong) Shopdata *shopdata;
//构造方法
- (instancetype)initWithShopdata:(Shopdata *)data;
+ (instancetype)shopWithshopdata:(Shopdata *)data;
@end

#import "Shopdata.h"
@interface ShopView ()
//定义两个控件的全局变量
@property (nonatomic, weak) UIImageView *iconView;
@property (nonatomic, weak) UILabel *nameView;
@end

@implementation ShopView

- (instancetype)init
{
    if (self = [super init]) {
        [self initialize];
//初始化数值
    }
    return self;
}

- (void)initialize{
//初始化里面的数据 创建2个控件的对象
    UIImageView *shopIconView = [[UIImageView alloc]init];
    shopIconView.backgroundColor = [UIColor blackColor];
    [self addSubview:shopIconView];
    _iconView = shopIconView;

    UILabel *shopNameView = [[UILabel alloc]init];
    shopNameView.backgroundColor = [UIColor whiteColor];
    shopNameView.textAlignment = NSTextAlignmentCenter;
    [self addSubview:shopNameView];
    _nameView = shopNameView;

}

-(void)layoutSubviews{
/*
控件的尺寸必须放在layoutSubviews里面,而且必须执行一次[super layoutSubviews]
*/
    [super layoutSubviews];
    CGFloat width = self.frame.size.width;
    CGFloat height = self.frame.size.height;
    self.iconView.frame = CGRectMake(0, 0, width, width);
    self.nameView.frame = CGRectMake(0, width, width, height - width);
}

- (void)setShopdata:(Shopdata *)shopdata
//接受数据 把数据类传入自定义控件类
{
    _shopdata = shopdata;
    self.iconView.image = [UIImage imageNamed:shopdata.icon];
    self.nameView.text = shopdata.name;

}

- (instancetype)initWithShopdata:(Shopdata *)data{
    if (self = [super init]) {
        // 注意:先创建后赋值
        [self initialize];
        self.shopdata = data;
    }
    return self;
}

+ (instancetype)shopWithShopdata:(Shopdata *)data
{
    return [[self alloc]initWithShopdata:data];
}

懒加载的含义是为了提高app初始化的速度,字典转换数据模型是为了减低出错率 提高封装性 构造方法和自定义控件方法还有提高封装性.今天的内容也就到这样了 附上demo截图
dayFour02

初八 继续学习!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值