美文网首页iOS9开发技术设计模式ios
iOS开发UI篇-懒加载、重写setter方法赋值

iOS开发UI篇-懒加载、重写setter方法赋值

作者: 河南蓝鸥科技有限公司 | 来源:发表于2015-11-29 22:54 被阅读8848次

一、懒加载

1.懒加载定义

懒加载——也称为延迟加载,即在需要的时候才加载(效率低,占用内存小)。所谓懒加载,写的是其get方法.

注意:如果是懒加载的话则一定要注意先判断是否已经有了,如果没有那么再去进行实例化

2.使用懒加载的好处:

(1)不必将创建对象的代码全部写在viewDidLoad方法中,代码的可读性更强

(2)每个控件的getter方法中分别负责各自的实例化处理,代码彼此之间的独立性强,松耦合

3.代码示例

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic, strong)UIImageView *imageView;//QQ图片

@property (nonatomic, strong)UILabel *userNameLabel;//用户名

@property (nonatomic, strong)UITextField *userNameTextField;//用户名输入框

@property (nonatomic, strong)UILabel *passwordLabel;//密码

@property (nonatomic, strong)UITextField *passwordTextField;//密码输入框

@end

@implementation ViewController

/** 懒加载

*  懒加载——也称为延迟加载,即在需要的时候才加载(效率低,占用内存小)。所谓懒加载,写的是其get方法;

*  图片控件的延迟加载

*/

- (UIImageView *)imageView {

//判断对象是否已经有了,如果没有,则进行实例化创建对象

if (_imageView == nil) {

self.imageView = [[UIImageView alloc]initWithFrame:CGRectMake(130, 80, self.view.frame.size.width - 260, 155)];

//添加图片

_imageView.image = [UIImage imageNamed:@"QQ.jpg"];

}

return _imageView;//返回图片控件对象

}

/**

*  懒加载

*  用户名标签控件的延迟加载

*/

- (UILabel *)userNameLabel {

//判断对象是否已经有了,如果没有,则进行实例化创建对象

if (_userNameLabel == nil) {

self.userNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(40, 265, 60, 40)];

_userNameLabel.adjustsFontSizeToFitWidth = YES;

_userNameLabel.text = @"用户名";

}

return _userNameLabel;

}

/**

*  懒加载

*  用户名输入框的延迟加载

*/

- (UITextField *)userNameTextField {

//判断对象是否已经存在,如果不存在,则进行实例化创建对象

if (_userNameTextField == nil) {

self.userNameTextField = [[UITextField alloc]initWithFrame:CGRectMake(120, 265, self.view.frame.size.width - 140, 40)];

_userNameTextField.placeholder = @"请输入用户名:";

_userNameTextField.borderStyle = UITextBorderStyleRoundedRect;

}

return _userNameTextField;

}

/**

*  懒加载

*  密码标签的懒加载

*/

- (UILabel *)passwordLabel {

//判断对象是否已经存在,如果不存在,则进行实例化创建对象

if (_passwordLabel == nil) {

self.passwordLabel = [[UILabel alloc]initWithFrame:CGRectMake(40, 325, 60, 40)];

_passwordLabel.text = @"密码";

_passwordLabel.adjustsFontSizeToFitWidth = YES;

}

return _passwordLabel;

}

/**

*  懒加载

*  密码输入框的懒加载

*/

- (UITextField *)passwordTextField {

//判断对象是否已经存在,如果不存在,则进行实例化创建对象

if (_passwordTextField == nil) {

self.passwordTextField = [[UITextField alloc]initWithFrame:CGRectMake(120, 325, self.view.frame.size.width - 140, 40)];

_passwordTextField.borderStyle = UITextBorderStyleRoundedRect;

_passwordTextField.placeholder = @"请输入密码:";

}

return _passwordTextField;

}

- (void)viewDidLoad {

[super viewDidLoad];

//将图片控件添加到视图控制器的根视图view上;

[self.view addSubview:self.imageView];

//将用户名控件添加到视图控制器的根视图view上

[self.view addSubview:self.userNameLabel];

//将用户名输入框控件添加到视图控制器的根视图view上

[self.view addSubview:self.userNameTextField];

//将密码控件添加到视图控制器的根视图view上

[self.view addSubview:self.passwordLabel];

//将密码输入框对象添加到视图控制器的根视图view上

[self.view addSubview:self.passwordTextField];

// Do any additional setup after loading the view, typically from a nib.

}

二、重写setter方法赋值

在UITableView中为cell上的控件赋值,可以在自定义的cell接口部分声明一个方法,然后在实现部分为cell上的控件赋值即可,不过,如果在接口部分写一个属性,然后,在对应的实现部分重写setter方法为cell上的控件赋值,这样在外界访问时就更加方便了;

代码实例如下:

@interface Person : NSObject

@property (nonatomic, strong)NSString *name;//姓名

@property (nonatomic, strong)NSString *phoneNumber;//电话号码

//自定义初始化方法

- (id)initWithName:(NSString *)name

phoneNumber:(NSString *)phoneNumber;

@end

#import "Person.h"

@implementation Person

- (id)initWithName:(NSString *)name phoneNumber:(NSString *)phoneNumber {

self = [super init];

if (self) {

self.name = name;

self.phoneNumber = phoneNumber;

}

return self;

}

#import#import "Person.h"

/*

定制cell

*/

@interface CustomCell : UITableViewCell

@property (nonatomic, strong)UILabel *nameLabel;//姓名

@property (nonatomic, strong)UILabel *phoneNumberLabel;//电话

@property (nonatomic, strong)Person *model;//属性,为cell上的控件赋值

@end

#import "CustomCell.h"

@implementation CustomCell

//重写初始化方法

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{

self  = [super initWithStyle:style reuseIdentifier:

reuseIdentifier];

if (self) {

//1.添加联系人姓名

self.nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 5, 110, 50)];

self.nameLabel.textAlignment = NSTextAlignmentLeft;

self.nameLabel.adjustsFontSizeToFitWidth = YES;

self.nameLabel.textColor = [UIColor blackColor];

[self.contentView addSubview:self.nameLabel];

//2.添加电话号码

self.phoneNumberLabel = [[UILabel alloc]initWithFrame:CGRectMake(140, 5, 200, 50)];

self.phoneNumberLabel.textAlignment = NSTextAlignmentLeft;

self.phoneNumberLabel.adjustsFontSizeToFitWidth = YES;

[self.contentView addSubview:self.phoneNumberLabel];

}

return self;

}

//重新setter方法,为cell上的控件赋值

- (void)setModel:(Person *)model {

if (_model != model) {

_model = model;

}

//为nameLabel赋值

self.nameLabel.text = model.name;

//为phoneNumberLabel赋值

self.phoneNumberLabel.text = model.phoneNumber;

}

@end

//在配置cell的方法里,调用setter方法,为cell的控件赋值;

//配置cell  设置cell上显示的内容,同时返回cell

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

//使用自定义cell

//1.创建静态标示符字符串

static NSString *cellIdentifier = @"CELL";

//2.根据重用标示符去重用队列里找对应类型的cell使用,(获取可重用的cell)

CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

//3.判断是否从重用队列中获取到可重用的cell,如果没有获取到,则需要重新创建相对应类型的cell对象

if (cell == nil) {

cell = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];

}

//让cell上的控件显示数据,为cell赋值

cell.model = [self.personArray objectAtIndex:indexPath.row];

return cell;

}

相关文章

  • iOS开发UI篇-懒加载、重写setter方法赋值

    一、懒加载 1.懒加载定义 懒加载——也称为延迟加载,即在需要的时候才加载(效率低,占用内存小)。所谓懒加载,写的...

  • 十九、Swift3.0之getter和setter 、计算型和只

    说明:Swift实际开发中一般不会重写setter和getter方法,这里仅供参考和了解。 这里顺便说一下懒加载和...

  • iOS 数组 NSArray 遍历 懒加载总结

    iOS开发之懒加载 iOS中数组遍历的方法及比较

  • 重写getter和setter方法或者“懒加载”?

    在开发中,尽量不要使用_name这种类型的调用,而是声明为属性,直接使用self.name这样的写法。声明为属性,...

  • Objective-C 之懒加载

    懒加载简介 懒加载又叫延时加载,指当对象需要用到的时候在去初始化,实现方法就是重写对象的get方法,当系统或者开发...

  • iOS 让一个只读属性,内部支持读写,外部只读。

    大致思路声明属性为只读,但是重写setter赋值方法,具体实现如下: import

  • iOS 懒加载

    懒加载:也称延时加载,即在对象用到的的时候才加载。其实懒加载,就是所谓的重写对象的get方法,当系统或者开发者调用...

  • ios

    使用懒加载重写getter方法,在方法里用到self.赋值,会造成死循环,self.写在等式左边不是调用的sett...

  • iOS开发,懒加载

    什么是懒加载? 懒加载--比较懒的加载方式,需要的时候才加载,也称为延时加载。 所谓懒加载既是重写get方法,一定...

  • iOS-UI-懒加载

    原文找不到了 iOS-UI-懒加载 1.懒加载基本 懒加载——也称为延迟加载,即在需要的时候才加载(效率低,占用内...

网友评论

  • 灯泡虫: 懒加载自动编码插件 http://www.jianshu.com/p/e6e6e10421aa 喜欢懒加载朋友们可以试试
  • LoveY34:我想问个问题,一个声明好的属性可以同时实现set和get方法吗?我试了,两个同时实现会报错!注释掉其中一个就不报错了!这是为什么?
    花开一时:@Lylhf 同时重写需要自己声明@synthesize <#property#>,不然系统不会再自动关联<#property#>和 _<#property#>.
  • MonkeyLu:self.imageView = [[UIImageView alloc]initWithFrame:CGRectMake(130, 80, self.view.frame.size.width - 260, 155)];(不会递归)

    _imageView = [[UIImageView alloc]initWithFrame:CGRectMake(130, 80, self.view.frame.size.width - 260, 155)];
    作者能区别下这两种懒加载方法吗
    MonkeyLu:@New_Driver 亲测,不会死循环
    New_Driver:@MonkeyLu 第一种点语法,会死循环. 懒加载要用第二种方法. 点语法就是调用setget方法, 还没初始化实例,又在get里面调用setget,是不行地!
  • 9ec3ab880a32:递归了!!!
  • 一路向东向西向南向北:懒加载内部调用该对象对应的点语法貌似会死循环:stuck_out_tongue_winking_eye:
    Leehf:必然死循环

本文标题:iOS开发UI篇-懒加载、重写setter方法赋值

本文链接:https://www.haomeiwen.com/subject/mzcjhttx.html