0

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

'UICollectionView 必须使用非零布局参数初始化'

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

我在哪里做错了?

@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;
}
4

2 回答 2

1

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

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

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

self.collectionView.collectionViewLayout = layout

于 2018-02-22T18:27:12.007 回答
0

解决了我的问题...感谢大家在此给我的许多提示和建议..您非常友善!

之所以会出现所有这些问题,是因为在UICollectionViewController演示时未使用其布局进行初始化。

我使用该方法UICollectionViewController从另一个人调用....此时问题发生了...UIViewController[self presentViewController:collectionViewController animated:YES completion:nil]

UICollectionViewController只分配给展示,没有必要的布局来展示......这就是为什么这个该死的崩溃!

所以我这样解决了

UIViewController.m

-(void)presentCollectionViewController {
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    layout.itemSize = CGSizeMake (100, 100);
    [layout setScrollDirection: UICollectionViewScrollDirectionHorizontal];
    
    UICollectionViewController * chooseRegion = [[UICollectionViewController alloc] initWithCollectionViewLayout:layout];

    [self presentViewController: chooseRegion animated: YES completion: nil];
}

UICollectionViewController.m

-(void)viewDidLoad {
    [super viewDidLoad];

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

}

在这一点上,一切正常......

在 UICollectionViewController 内部,在这种情况下,您不需要指定任何类型的布局,因为它是在展示之前的类分配期间指定的

于 2018-02-22T18:26:44.233 回答