ios - UICollectionViewController 与流布局崩溃

标签 ios objective-c uicollectionview uicollectionviewlayout uicollectionviewflowlayout

我刚刚以编程方式创建了一个UICollectionViewController。到目前为止,一切都很好,但我面临一些问题。当调用 CollectionViewController 时,我的应用程序立即崩溃,返回此错误。

'UICollectionView must be initialized with a non-nil layout parameter'

我当然尝试插入UICollectionViewFlowLayout,但它继续崩溃,我不明白为什么。有人可以向我解释如何在不使用 Storyboard的情况下使用 UICollectionViewController 吗??

我哪里做错了?

@implementation ChooseRegion

static NSString * const reuseIdentifier = @"Cell";

- (void)viewDidLoad {
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = NO;

    // Register cell classes
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];

    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    layout.itemSize = CGSizeMake(100, 100);

    self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:layout];

    // Do any additional setup after loading the view.
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
#warning Incomplete implementation, return the number of sections
    return 0;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of items
    return 0;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];

    // Configure the cell

    return cell;
}

最佳答案

如果您正在使用(或子类化)UICollectionViewController,则它已经分配了一个UICollectionView 实例。您不需要实例化或创建一个新的。我怀疑这不起作用的原因是因为您没有将新 View 添加到层​​次结构中(即使您将其分配给 self.collectionView

代替 self.collectionView = [[UICollectionView alloc] init...

只需将布局分配给您现有的 UICollectionView,例如:

self.collectionView.collectionViewLayout = 布局

关于ios - UICollectionViewController 与流布局崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48934059/

相关文章:

ios - 如何在elcImagePickerController中获取视频的NSData?

iphone - 如何测量 iPhone 中滑动的速度,然后计算图像的旋转?

iphone - 减小图像文件大小

swift - UICollection View 与 TableView 错误索引超出范围为什么

swift - 将 UICollectionView 滚动到底部

ios - FBSDKSettings.setAutoLogAppEventsEnabled(true) 方法在哪里?

ios - 从所有(可见和不可见)UICollectionViewCell 获取框架

objective-c - 整体亮度/ Gamma ?

iphone - 如何在 Obj-c 中获取搜索到的文件夹的父目录?

ios - 如何解决收藏 View 闪烁问题? ( swift )