2023年7月14日发(作者:)
iOS开发UI篇-懒加载、重写setter⽅法赋值⼀、懒加载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) {iew = [[UIImageView alloc]initWithFrame:CGRectMake(130, 80, - 260, 155)];//添加图⽚_ = [UIImage imageNamed:@""];}return _imageView;//返回图⽚控件对象}/*** 懒加载* ⽤户名标签控件的延迟加载*/- (UILabel *)userNameLabel {//判断对象是否已经有了,如果没有,则进⾏实例化创建对象if (_userNameLabel == nil) {meLabel = [[UILabel alloc]initWithFrame:CGRectMake(40, 265, 60, 40)];_sFontSizeToFitWidth = YES;_ = @"⽤户名";}return _userNameLabel;}/*** 懒加载* ⽤户名输⼊框的延迟加载*/- (UITextField *)userNameTextField {//判断对象是否已经存在,如果不存在,则进⾏实例化创建对象if (_userNameTextField == nil) {meTextField = [[UITextField alloc]initWithFrame:CGRectMake(120, 265, - 140, 40)];_older = @"请输⼊⽤户名:";_Style = UITextBorderStyleRoundedRect;}return _userNameTextField;}/*** 懒加载* 密码标签的懒加载*/- (UILabel *)passwordLabel {//判断对象是否已经存在,如果不存在,则进⾏实例化创建对象if (_passwordLabel == nil) {rdLabel = [[UILabel alloc]initWithFrame:CGRectMake(40, 325, 60, 40)];_ = @"密码";_sFontSizeToFitWidth = YES;}return _passwordLabel;}/*** 懒加载* 密码输⼊框的懒加载*/- (UITextField *)passwordTextField {//判断对象是否已经存在,如果不存在,则进⾏实例化创建对象if (_passwordTextField == nil) {rdTextField = [[UITextField alloc]initWithFrame:CGRectMake(120, 325, - 140, 40)];_Style = UITextBorderStyleRoundedRect;_older = @"请输⼊密码:";}return _passwordTextField;}- (void)viewDidLoad {[super viewDidLoad];//将图⽚控件添加到视图控制器的根视图view上;[ addSubview:iew];//将⽤户名控件添加到视图控制器的根视图view上[ addSubview:meLabel];//将⽤户名输⼊框控件添加到视图控制器的根视图view上[ addSubview:meTextField];//将密码控件添加到视图控制器的根视图view上[ addSubview:rdLabel];//将密码输⼊框对象添加到视图控制器的根视图view上[ addSubview:rdTextField];// 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 *)namephoneNumber:(NSString *)phoneNumber;@end#import "Person.h"@implementation Person- (id)initWithName:(NSString *)name phoneNumber:(NSString *)phoneNumber {self = [super init];if (self) { = name;umber = 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.添加联系⼈姓名bel = [[UILabel alloc]initWithFrame:CGRectMake(20, 5, 110, 50)];ignment = NSTextAlignmentLeft;sFontSizeToFitWidth = YES;lor = [UIColor blackColor];[tView addSubview:bel];//2.添加电话号码umberLabel = [[UILabel alloc]initWithFrame:CGRectMake(140, 5, 200, 50)];ignment = NSTextAlignmentLeft;sFontSizeToFitWidth = YES;[tView addSubview:umberLabel];}return self;}//重新setter⽅法,为cell上的控件赋值- (void)setModel:(Person *)model {if (_model != model) {_model = model;}//为nameLabel赋值 = ;//为phoneNumberLabel赋值 = umber;}@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赋值 = [Array objectAtIndex:];return cell;}
两种懒加载⽅法区别:第⼀种点语法,会死循环. 懒加载要⽤第⼆种⽅法. 点语法就是调⽤set和get⽅法, 还没初始化实例,⼜在get⾥⾯调⽤setget,是不⾏地!iew = [[UIImageView alloc]initWithFrame:CGRectMake(130, 80, - 260, 155)];(不会递归)_imageView = [[UIImageView alloc]initWithFrame:CGRectMake(130, 80, - 260, 155)];⼀个声明好的属性可以同时实现set和get⽅法吗?我试了,两个同时实现会报错!注释掉其中⼀个就不报错了!这是为什么?同时重写需要⾃⼰声明@synthesize <#property#>,不然系统不会再⾃动关联<#property#>和 _<#property#>.
发布者:admin,转转请注明出处:http://www.yc00.com/xiaochengxu/1689264654a226432.html
评论列表(0条)