【问题标题】:UICollectionViewController Programmatically crashing with non-nil layout parameter errorUICollectionViewController 以编程方式因非零布局参数错误而崩溃
【发布时间】:2013-04-02 22:07:04
【问题描述】:

我正在以编程方式创建UICollectionViewController。不,我不想像每个教程一样使用 IB 和 Storyboard。我只想使用纯代码。

这是我在viewDidLoad 中的内容:

// UICollectionView
    UICollectionViewFlowLayout *aFlowLayout = [[UICollectionViewFlowLayout alloc] init];
    [aFlowLayout setItemSize:CGSizeMake(200, 140)];
    [aFlowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
    self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:aFlowLayout];
    [self.collectionView setDelegate:self];
    [self.collectionView setDataSource:self];
    self.collectionView.backgroundColor = [UIColor clearColor];
    [self.collectionView setAutoresizingMask:UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth];
    [self.collectionView registerClass:[PUCImageGridCell class] forCellWithReuseIdentifier:CollectionViewCellIdentifier];

我已经实现了所需的委托方法,但我仍然收到此错误:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'UICollectionView must be initialized with a non-nil layout parameter'

【问题讨论】:

    标签: ios objective-c cocoa-touch uicollectionview


    【解决方案1】:

    这是我用来初始化UICollectionViewController的:

    - (id)initWithCollectionViewLayout:(UICollectionViewLayout *)layout
    

    您不需要实例化collectionView,控制器会为您完成。在调用它之后设置框架,或者您可以覆盖并设置内部框架:

    - (id)initWithCollectionViewLayout:(UICollectionViewLayout *)layout{
         self = [super initWithCollectionViewLayout:layout];
         if (self) {
             // additional setup here if required.
         }
         return self;
    }
    

    我更喜欢使用autolayout 约束。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-03-31
      • 1970-01-01
      • 1970-01-01
      • 2021-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多