美文网首页
iOS中的懒加载

iOS中的懒加载

作者: 幸福已倒带 | 来源:发表于2016-12-26 20:10 被阅读93次

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:CGRectZerocollectionViewLayout: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.0blue: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方法,造成死循环)总结:懒加载即用到时方去加载对象

相关文章

  • Swift的懒加载和只读属性的介绍

    懒加载 在 iOS 开发中,懒加载是无处不在的 懒加载的格式如下: lazy var person: Person...

  • 2018-12-21

    iOS开发中懒加载遇到的坑 正常写一个懒加载对象 - (MAMapView*)mapView{ if(nil=...

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

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

  • iOS开发之懒加载

    iOS开发之懒加载 在iOS开发中几乎经常用到懒加载技术,比如我们存放网络数据的数组,控制器的view,控件的自定...

  • # 懒加载

    在 iOS 开发中,懒加载是无处不在的 懒加载的格式如下: 懒加载本质上是一个闭包 以上代码可以改写为以下格式 懒...

  • iOS中的懒加载

    iOS开发中 我们经常使用懒加载1.懒加载的好处,让控件和对象在最需要加载的时候加载。这样可以节省内存空间,因为我...

  • IOS中的懒加载

    什么是懒加载: 也称为延迟加载,即在需要的时候才加载(效率低,占用内存小) (swift 控件的懒加载 (就是一个...

  • iOS-UI-懒加载

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

  • OC方法的懒加载

    前言 iOS中,大家都比较熟悉属性的懒加载,其实OC方法也能懒加载。在程序运行时,去加载指定方法,可以起到节约性能...

  • SwiftUI 中的List 在MacOS 中的性能优化。

    List在iOS中有懒加载的特性,但是在MacOS中会一次性加载完List中的所有的数据。并没有懒加载的特性。 所...

网友评论

      本文标题:iOS中的懒加载

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