您的位置:首页 > 移动开发 > IOS开发

iOS中的懒加载

2016-05-10 08:37 369 查看
iOS开发中 我们经常使用懒加载

1.懒加载的好处,让控件和对象在最需要加载的时候加载。这样可以节省内存空间,因为我们移动的设备资源还是比较宝贵的。所谓懒加载 就是推迟他的getter方法的执行。

比如。一个view的子控件 ,只有当这个view被显示的时候才去加载。一个tableViewCell中,给他设置了图片,他的content View里面才包含imageView的图片,只有设置了textLabel的内容,才会加载这个textLabel.

//collectionView的数据源--属性申明
@property (nonatomic,
strong) NSArray *famouseDoctorArr;

//懒加载
-(NSArray *)famouseDoctorArr{

    if (_famouseDoctorArr ==
nil) {

        _famouseDoctorArr =[FDFamousDoctorsTong
famousDoctorsTongArr];
    }

    return
_famouseDoctorArr;
}
// collectionView的懒加载属性申明

@property (nonatomic,
strong) UICollectionView *collectionView;

-(UICollectionView *)collectionView{

    if (_collectionView ==
nil) {

        

        _collectionView = [[UICollectionView
alloc]initWithFrame:CGRectZero
collectionViewLayout:self.layout];

        _collectionView.backgroundColor =[UIColor
orangeColor];
       
// 指定代理

        _collectionView.dataSource =
self;

        _collectionView.delegate =
self;
       
//注册cell

        //    [self.collectionView registerClass:[FDIllCollectionViewCell class] forCellWithReuseIdentifier:@"illCell"];

        [_collectionView
registerNib:[UINib
nibWithNibName:@"FDIllCollectionViewCell"
bundle:nil]
forCellWithReuseIdentifier:@"illCell"];

        
    }

    return
_collectionView;
}

-----说说我今天在懒加载中遇到的问题
//1.0---我的illView中 collection View中的 cell被点击了。

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

    //把模型传递给下一个控制器

    FDFamousDoctorsTong *famouseDoctorTong =
self.famouseDoctorArr[indexPath.item];

    if (indexPath.item ==
self.famouseDoctorArr.count -
1) {

        NSLog(@"加载公益活动界面");

    }else{

        //跳转到疾病选择控制器 --通知主页控制器切换

        if ([self.delegate
respondsToSelector:@selector(illViewDidSelectItem:)]) {

            [self.delegate
illViewDidSelectItem:famouseDoctorTong];

        }    

    }

}

// 在获取这个illView中控制器中实现代理方法

#pragma mark - 疾病视图cell被点击的回调代理方法  ---执行跳转方法 传递模型

-(void)illViewDidSelectItem:(FDFamousDoctorsTong *)famouseDodctorTong{

    //
从storyBoard里面加载Controller

    UIStoryboard *sb =[UIStoryboard
storyboardWithName:@"FDIllDetailViewController"
bundle:nil];

    

    //FDIllDetailViewController *detailVc =[sb instantiateViewControllerWithIdentifier:@"detailVC"];

    

    FDIllDetailViewController *detailVc = sb.instantiateInitialViewController;

    detailVc.title =
@"疾病选择详情";

    detailVc.famousDoctorsTong = famouseDodctorTong;

    detailVc.view.backgroundColor =[UIColor
colorWithRed:241/255.0
green:241/255.0
blue:241/255.0
alpha:1.0];

    [self.navigationController
pushViewController:detailVc animated:YES];

    

}

//3.0重写模型的setter方法給控件的属性赋值

#pragma mark - 从主页控制器点击疾病跳转的得到参数重写模型的setter方法

-(void)setFamousDoctorsTong:(FDFamousDoctorsTong *)famousDoctorsTong{

    _famousDoctorsTong = famousDoctorsTong;

    NSLog(@"famousDoctorsTong.title:%@",famousDoctorsTong.title);

    
    self.illTypeLabel.text = [NSString
stringWithFormat:@"疾病类型:%@",famousDoctorsTong.title];

 

}

好了看似没有什么问题,但是打断点 发现 self.illTypeLabel 这个控件是nil
为什么 调用setter方法的时候还没有加载这个控制器的view,所以子控件也是没有加载的
----解决办法 把赋值的代码方法放到viewDidLoad里面去

- (void)viewDidLoad {

    [super
viewDidLoad];

    

    //TODO测试

    

    self.treatedMethodView.hidden =
YES;

    

    [self.illDetailBtn
setBackgroundColor:[UIColor
clearColor]];

    [self.complicateBtn
setBackgroundColor:[UIColor
clearColor]];

    [self.selectTreatMethodBtn
setBackgroundColor:[UIColor
clearColor]];

    

        self.illTypeLabel.text =[NSString
stringWithFormat:@"疾病类型:%@",self.famousDoctorsTong.title];

    

  }

// 最后来一个总结

懒加载的优点
不需将对象的实例化写到viewDidLoad,可以简化代码,增强代码的可读性
对象的实例化在getter方法中,各司其职,降低耦合性
对系统的内存占用率会减小

viewDidLoad正常加载代码示例

没用懒加载的时候,从plist获取数据,返回一个数组,需要写在viewDidLoad方法中获取
@interface ViewController ()

@property (nonatomic, strong) NSArray *shopData;

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

_shopData = [NSArray arrayWithContentsOfFile:
[[NSBundle mainBundle] pathForResource:@"shop" ofType:@"plist"]];
}

@end

显而易见,当控制器被加载完成后就会加载当前的shopData,假如shopData是在某些事件被触发的时候才会被调用,没必要在控制器加载完就去获取plist文件,如果事件不被触发,代表着shopData永远不会被用到,这样在viewDidLoad中加载shopData就会十分多余,并且耗用内存

懒加载代码示例

- (void)viewDidLoad {

[super viewDidLoad];
}

- (NSArray *)shopData
{
if (!_shopData) {
_shopData = [NSArray arrayWithContentsOfFile:
[[NSBundle mainBundle] pathForResource:@"shop" ofType:@"plist"]];
}
return _shopData;
}

@end

当需要用到shopData的时候,就会调用[self shopData]的方法(即getter方法),此时系统会去调用getter方法,然后再getter方法中获取plist文件内容,然后返回使用(需要注意在getter方法里切勿使用self.shopData,因为self.shopData会调用getter方法,造成死循环)

总结:懒加载即用到时方去加载对象

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios开发 懒加载