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) {
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#>.
iOS开发UI篇-懒加载、重写setter方法赋值的更多相关文章
- iOS开发UI篇—懒加载
iOS开发UI篇—懒加载 1.懒加载基本 懒加载——也称为延迟加载,即在需要的时候才加载(效率低,占用内存小).所谓懒加载,写的是其get方法. 注意:如果是懒加载的话则一定要注意先判断是否已经有了, ...
- iOS开发UI中懒加载的使用方法
1.懒加载基本 懒加载——也称为延迟加载,即在需要的时候才加载(效率低,占用内存小).所谓懒加载,写的是其getter方法.说的通俗一点,就是在开发中,当程序中需要利用的资源时.在程序启动的时候不加载 ...
- iOS开发——UI基础-懒加载,plist文件,字典转模型,自定义view
一.懒加载 只有使用到了商品数组才会创建数组 保证数组只会被创建一次 只要能够保证数组在使用时才创建, 并且只会创建一次, 那么我们就称之为懒加载 lazy - (void)viewDidLoad 控 ...
- iOS开发UI篇—懒载入
iOS开发UI篇-懒载入 1.懒载入基本 懒载入--也称为延迟载入,即在须要的时候才载入(效率低,占用内存小).所谓懒载入,写的是其get方法. 注意:假设是懒载入的话则一定要注意先推断是否已经有了. ...
- UI篇—懒加载
1.懒加载基本 懒加载——也称为延迟加载,即在需要的时候才加载(效率低,占用内存小).所谓懒加载,写的是其get方法. 注意:如果是懒加载的话则一定要注意先判断是否已经有了,如果没有那么再去进行实例化 ...
- ios开发UI篇—使用纯代码自定义UItableviewcell实现一个简单的微博界面布局
本文转自 :http://www.cnblogs.com/wendingding/p/3761730.html ios开发UI篇—使用纯代码自定义UItableviewcell实现一个简单的微博界面布 ...
- iOS开发UI篇—UITableview控件使用小结
iOS开发UI篇—UITableview控件使用小结 一.UITableview的使用步骤 UITableview的使用就只有简单的三个步骤: 1.告诉一共有多少组数据 方法:- (NSInteger ...
- iOS开发UI篇—使用UItableview完成一个简单的QQ好友列表(一)
iOS开发UI篇—使用UItableview完成一个简单的QQ好友列表(一) 一.项目结构和plist文件 二.实现代码 1.说明: 主控制器直接继承UITableViewController // ...
- iOS开发UI篇—使用picker View控件完成一个简单的选餐应用
iOS开发UI篇—使用picker View控件完成一个简单的选餐应用 一.实现效果 说明:点击随机按钮,能够自动选取,下方数据自动刷新. 二.实现思路 1.picker view的有默认高度为162 ...
随机推荐
- Bootstrap_表单_按钮
一.多标签支持 一般制作按钮除了使用<button>标签元素之外,还可以使用<input type="submit">和<a>标签等. 同样,在 ...
- 入门5:PHP 语法基础——流程控制
一.if...else 语句 if( ) else{ } 如果 .... 就.... 否则.... if(判断){ 判断成立 则执行该表达式 }else{ 如果上方判断都不成立 则执行该表达式 } i ...
- YII 自动引入juquery进行表单验证
在form表单 里面引入这么一句话 array( 'enableClientValidation'=>true, 'clientOptions'=>array( ...
- Python Tutorial 学习(四)--More Control Flow Tools
4.1 if 表达式 作为最为人熟知的if.你肯定对这样的一些表达式不感到陌生: >>> x = int(raw_input("Please enter an intege ...
- uboot的devices_init函数分析
一.函数说明 函数功能: 完成设备的初始化 函数位置: common/devices.c 二.程序分析 int devices_init (void) { #ifndef CONFIG_ARM /* ...
- 3、MyBatis.Net学习笔记之增删改
增删改之前先说一下笔记1里提到的一个无法创建ISqlMapper对象的问题. <resultMaps> <resultMap id="FullResultMap" ...
- Ajax页面逻辑
逻辑上模拟整个与服务器通信的过程.在没有真正与服务器通信的时候,如何写这样的ajax请求. 先根据页面结构创建一个静态数据(JSON) var arrival_address_data={" ...
- codeforces C. Devu and Partitioning of the Array
题意:给你n个数,然后分成k部分,每一个部分的和为偶数的有p个,奇数的有k-p个,如果可以划分,输出其中的一种,不可以输出NO; 思路:先输出k-p-1个奇数,再输出p-1个偶数,剩余的在进行构造. ...
- AlgorithmsI Exercises: Analysis of Algorithms
Question 1 Suppose that you time a program as a function of N and producethe following table. N seco ...
- Cut the Cake(大数相乘)
MMM got a big big big cake, and invited all her M friends to eat the cake together. Surprisingly o ...