iOS懒加载不执行的坑

被绊倒了两次的坑,第一次使用懒加载就发现方法一直不执行,后来鼓捣了许久才好,第二次把上次的代码原原本本的复制过来,发现又不执行了,后来发现是调用出现了问题,上代码:

-(UICollectionView *)collectionView1
{
    if (!_collectionView1) {
        UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayout alloc] init];
        [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
        flowLayout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);//设置section的边距

        _collectionView1 = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, _myScrollView.height) collectionViewLayout:flowLayout];
        _collectionView1.delegate = self;
        _collectionView1.dataSource = self;
        _collectionView1.backgroundColor =RGBCOLOR(255, 255, 255);
        //注册
        [_collectionView1 registerClass:[mainCollectionCell class]forCellWithReuseIdentifier:@"collectionCell"];
    }
    return _collectionView1;
}

懒加载方法这样写没有问题,注意第一次调用的时候一定要用self.XXX,_XXX是没有用的:

[self.myScrollView addSubview:self.collectionView1];

猜你喜欢

转载自blog.csdn.net/c_chang/article/details/76212140