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];
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章