iOS 懒加载

一、控件的懒加载,如下:

-(HorizontalMenuView*)horizontalView{

if(!_horizontalView){

_horizontalView= [[HorizontalMenuView alloc] initWithFrame:CGRectMake(10, 69,Main_Screen_Width- 20,Main_Screen_Height * 0.045)];

NSArray*arr = @[@"门店视频",@"在线直播"];

[_horizontalView setNameWithArray:arr WithTag:0];

_horizontalView.layer.cornerRadius=VIEW_HEIGHT/ 2;

_horizontalView.layer.masksToBounds=YES;

_horizontalView.backgroundColor=BASE_APP_COLOR_DEEP;

}

return_horizontalView;

}

2、tableView的懒加载:

-(UITableView*)tableView{

if(!_tableView){

_tableView= [[UITableView alloc] initWithFrame:CGRectMake(10,MaxY(_horizontalView) + 5,Main_Screen_Width- 20, 200)];

_tableView.backgroundColor=BASE_APP_COLOR_DEEP;

_tableView.delegate=self;

_tableView.dataSource=self;

}

return_tableView;

}

3、一定一定要注意的坑:

懒加载后并不是就完了,一样要去调用的。

怎么调用呢?

如果是在控制器,那么在viewDidLoad方法中:

[self.view addSubview:self.horizontalView];

[self.view addSubview:self.tableView];

这就算是调用了。

注意这里一定要是self.tableView,而不能是_tableView。

二、数组的懒加载:

-(NSArray*)dataArray{

if(!_dataArray){

NSArray*arr= [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"StoreVideo" ofType:@"plist"]];

self.dataArray= arr;

}

return_dataArray;

}

2、那么数组的懒加载,注意点在于它一样要被调用。

怎么调用呢?

在你要用到这个数组的地方,把它写成self.dataArray,就是调用了。

如,在

-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView{

return self.dataArray.count;

}

你可能感兴趣的:(iOS 懒加载)