您的位置:首页 > 产品设计 > UI/UE

iOS开发——UI基础-懒加载,plist文件,字典转模型,自定义view

2015-07-15 13:10 567 查看

一、懒加载

只有使用到了商品数组才会创建数组

保证数组只会被创建一次

只要能够保证数组在使用时才创建, 并且只会创建一次, 那么我们就称之为懒加载 lazy

- (void)viewDidLoad 控制器的view创建完毕就会调用,该方法只会调用一次

@property (nonatomic, strong)NSArray *shops;

- (void)viewDidLoad
{
[super viewDidLoad];

if (self.shops == nil) {
NSLog(@"创建商品数组");
self.shops = @[
@{@"name":@"单肩包",
@"icon":@"danjianbao"},
@{@"name":@"链条包",
@"icon":@"liantiaobao"},
@{@"name":@"钱包",
@"icon":@"qianbao"},
@{@"name":@"手提包",
@"icon":@"shoutibao"}
];
}
}


二、plist文件



向plist文件写入

[_shops writeToFile:@"/Users/用户名/Desktop/shops.plist" atomically:YES];

_shops = [NSArray arrayWithContentsOfFile:@"/Users/用户名/Desktop/shops.plist"];


读取plist文件

// 1.获取plist文件的绝对路径
NSString *path = [[NSBundle mainBundle] pathForResource:@"shops.plist" ofType:nil];
// 2.根据路径加载plist文件
_shops = [NSArray arrayWithContentsOfFile:path];


注意点:

  在自定义plist文件的时候, 一定不能将plist文件的名称命名为info.plist, 或者xxxxinfo.plist

  也就是说自定义的plist文件的名称不能包含info这个单词

三、字典转模型

废话不多说,直接上代码

@interface ViewController ()
@property (nonatomic, strong)NSMutableArray *shops;
@end

/***************模型类***************/
@interface NJShop : NSObject
// 商品名称
@property(nonatomic, copy)NSString *name;
// 商品图片
@property(nonatomic, copy)NSString *icon;

+ (instancetype)shopWithDict:(NSDictionary *)dict;
@end

@implementation NJShop

+ (instancetype)shopWithDict:(NSDictionary *)dict
{
NJShop *shop = [[self alloc] init];
shop.name = dict[@"name"];
shop.icon = dict[@"icon"];
return shop;
}
@end

@implementation ViewController

// 重写getter方法
- (NSMutableArray *)shops
{
if (_shops == nil) {
// 1.获取plist文件的绝对路径
NSString *path = [[NSBundle mainBundle] pathForResource:@"shops.plist" ofType:nil];
// 2.根据路径加载plist文件
NSArray *tempArr = [NSArray arrayWithContentsOfFile:path];

// 3.将数组中所有的字典转换为模型
_shops = [NSMutableArray array];
for (NSDictionary *dict in tempArr) {
NJShop *shop = [NJShop shopWithDict:dict];
[_shops addObject:shop];
}
}
return _shops;
}
@end


在开发中一般不会直接从字典中获取数据

1.字典的key是一个字符串, 写错不会报错

2.英语不好, 单词记不住

3.由于key是一个字符串, 所以在编码的时候没有提示

为了解决这个问题, 我们可以使用对象来保存数据

// 1.创建一个父控件
UIView *containerView = [[UIView alloc] init];
containerView.backgroundColor = [UIColor redColor];
containerView.frame = CGRectMake(shopX, shopY, 70, 100);

// 2.创建一张图片
UIImageView *iv = [[UIImageView alloc] init];
iv.frame = CGRectMake(0, 0, 70, 70);

// 3.创建一个文本
UILabel *lable = [[UILabel alloc] init];
lable.frame = CGRectMake(0, 70, 70, 30);
lable.textAlignment = NSTextAlignmentCenter;

// 4.将图片和文本添加到父控件中
[containerView addSubview:iv];
[containerView addSubview:lable];

// 5.设置数据
NJShop *shop = self.shops[index];
UIImage *image = [UIImage imageNamed:shop.icon];
iv.image = image;
lable.text = shop.name;


如果当前对象的作用就是用于存储数据, 那么我称这个对象为模型

四、自定义view

@interface NJShopView : UIView
// 数据模型
@property(nonatomic, strong)NJShop *shop;
@end

@interface NJShopView ()
// ARC中如果是strong, 对象就不会释放, 如果是weak对象会自动释放
// strong强指针 weak弱指针

@property(nonatomic, weak)UIImageView *iv;

@property(nonatomic, weak)UILabel *lable;

@end

@implementation NJShopView

- (instancetype)init
{
if (self = [super init]) {
// 注意: 如果自定义一个View, 不建议在init方法中设置子控件的位置
// 因为如果子控件的位置需要根据父控件的frame来计算, 在init方法中拿不到父控件的frame

// 1.创建一张图片
// 注意: 千万不能使用一个弱指针的属性直接保存一个控件 \
否则对象创建出来立刻就会被释放
//        self.iv = [[UIImageView alloc] init];

UIImageView *iv = [[UIImageView alloc] init];
iv.backgroundColor = [UIColor yellowColor];
//        iv.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.width);
[self addSubview:iv];
// 这里可以使用weak的属性保存控件的原因, 是因为在前面已经将控件添加到父控件中了\
只要将一个对象添加到父控件中, 那么父控件中的subViews数组就会强引用这这个控件
self.iv = iv;

// 2.创建一个文本
UILabel *lable = [[UILabel alloc] init];
lable.backgroundColor = [UIColor purpleColor];
//        lable.frame = CGRectMake(0, self.frame.size.width, self.frame.size.width, self.frame.size.height - iv.frame.size.height);
lable.textAlignment = NSTextAlignmentCenter;
[self addSubview:lable];
self.lable = lable;
}
return self;
}

// layoutSubviews方法是专门用于布局子控件的位置的
// 注意: 重写layoutSubviews方法, 一定要调用[super layoutSubviews]方法 \
如果不调用, 会出现一些奇葩的错误
- (void)layoutSubviews
{
[super layoutSubviews];

CGFloat shopViewWidth = self.frame.size.width;
CGFloat shopViewHeight = self.frame.size.height;
// 1.布局图片的位置
self.iv.frame = CGRectMake(0, 0, shopViewWidth, shopViewWidth);
// 2.布局文本的位置
self.lable.frame = CGRectMake(0, shopViewWidth, shopViewWidth, shopViewHeight - self.iv.frame.size.height);
}

- (void)setShop:(NJShop *)shop
{
_shop = shop;

// 设置子控件的数据
self.iv.image = [UIImage imageNamed:_shop.icon];
self.lable.text = _shop.name;
}
@end

@interface ViewController ()
// 添加方法
- (IBAction)add;
// 移除方法
- (IBAction)remove;
// 商品容器
@property (weak, nonatomic) IBOutlet UIView *shopsView;

@property (weak, nonatomic) IBOutlet UIButton *removeBtn;
@property (weak, nonatomic) IBOutlet UIButton *addBtn;

@property (nonatomic, strong)NSMutableArray *shops;
@end

@implementation ViewController

- (IBAction)add
{
NJShopView *shopView = [[NJShopView alloc] init];
shopView.backgroundColor = [UIColor redColor];
// shopX, shopY
shopView.frame = CGRectMake(shopX, shopY, 70, 100);
[self.shopsView addSubview:shopView];

// 设置数据
//    [shopView setShop:self.shops[index]];
shopView.shop = self.shops[index];
}

// 重写getter方法
- (NSMutableArray *)shops
{
if (_shops == nil) {
NSLog(@"创建一个新的数组");
// 1.获取plist文件的绝对路径
NSString *path = [[NSBundle mainBundle] pathForResource:@"shops.plist" ofType:nil];
// 2.根据路径加载plist文件
NSArray *tempArr = [NSArray arrayWithContentsOfFile:path];

// 3.将数组中所有的字典转换为模型
_shops = [NSMutableArray array];
for (NSDictionary *dict in tempArr) {
NJShop *shop = [[NJShop alloc] init];
shop.name = dict[@"name"];
shop.icon = dict[@"icon"];
[_shops addObject:shop];
}

}
return _shops;
}
@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: