
一、懒加载
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(, , self.view.frame.size.width - , )]; //添加图片 _imageView.image = [UIImage imageNamed:@"QQ.jpg"]; } return _imageView;//返回图片控件对象 } /** * 懒加载 * 用户名标签控件的延迟加载 */ - (UILabel *)userNameLabel { //判断对象是否已经有了,如果没有,则进行实例化创建对象 if (_userNameLabel == nil) { self.userNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(, , , )]; _userNameLabel.adjustsFontSizeToFitWidth = YES; _userNameLabel.text = @"用户名"; } return _userNameLabel; } /** * 懒加载 * 用户名输入框的延迟加载 */ - (UITextField *)userNameTextField { //判断对象是否已经存在,如果不存在,则进行实例化创建对象 if (_userNameTextField == nil) { self.userNameTextField = [[UITextField alloc]initWithFrame:CGRectMake(, , self.view.frame.size.width - , )]; _userNameTextField.placeholder = @"请输入用户名:"; _userNameTextField.borderStyle = UITextBorderStyleRoundedRect; } return _userNameTextField; } /** * 懒加载 * 密码标签的懒加载 */ - (UILabel *)passwordLabel { //判断对象是否已经存在,如果不存在,则进行实例化创建对象 if (_passwordLabel == nil) { self.passwordLabel = [[UILabel alloc]initWithFrame:CGRectMake(, , , )]; _passwordLabel.text = @"密码"; _passwordLabel.adjustsFontSizeToFitWidth = YES; } return _passwordLabel; } /** * 懒加载 * 密码输入框的懒加载 */ - (UITextField *)passwordTextField { //判断对象是否已经存在,如果不存在,则进行实例化创建对象 if (_passwordTextField == nil) { self.passwordTextField = [[UITextField alloc]initWithFrame:CGRectMake(, , self.view.frame.size.width - , )]; _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(, , , )]; 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(, , , )]; 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; }
两种懒加载方法区别:第一种点语法,会死循环. 懒加载要用第二种方法. 点语法就是调用set和get方法, 还没初始化实例,又在get里面调用setget,是不行地!
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)];
一个声明好的属性可以同时实现set和get方法吗?我试了,两个同时实现会报错!注释掉其中一个就不报错了!这是为什么?
同时重写需要自己声明@synthesize <#property#>,不然系统不会再自动关联<#property#>和 _<#property#>.