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方法,造成死循環)總結:懶加載即用到時方去加載對象