1

我正在尝试以编程方式将 UICollectionView 添加到主视图中。

我首先创建了一个“PhotosViewController.h/h/xib”文件(类型为 UIViewController),删除了给出的视图并添加了一个UICollectionView作为该控制器的主视图,然后将“PhotosViewController.h”的超类更改为 UICollectionViewController .

我按照本教程中给出的前 6 个步骤进行操作:http: //skeuo.com/uicollectionview-custom-layout-tutorial然后跳到第 14 步。我要做的只是在用户单击时显示此视图在分段控制按钮上。

if ([segmentedControl selectedSegmentIndex]==3){
    //Photos View Controller
    pvc=[[PhotosViewController alloc] init];
    NSLog(@"Photos segment is chosen!");
    [[self view] addSubview:pvc.view]; **//Line 5**
}

但是程序在到达Line 5时抛出异常。

这是我收到的日志消息。

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

看起来,我在这里遗漏了一些非常基本的东西。在过去的一个小时里,我一直在寻找任何解决这个问题的方法,但我找不到任何东西。

任何帮助深表感谢。

4

2 回答 2

1
[[PhotosViewController alloc] init];

I assume this is where you are creating your collection view controller. The designated initialiser for this class is initWithCollectionViewLayout:, where you'd pass in a layout object. You aren't passing one in, so it complains. Try using that initialiser, or, in your subclass, make sure you call this instead of [super init], and pass in a new flow layout object.

于 2013-04-10T06:35:37.550 回答
0

我不清楚你在哪里初始化你的集合视图,但它应该看起来像这样:

UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
[flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];

collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0.0f,
                                                                    230.0f,
                                                                    [self viewWidth],
                                                                    [self viewHeight]) 
                                    collectionViewLayout:flowLayout];

除非你有更扩展的 UICollectionViewFlowLayout。

从您收到的消息来看,您似乎没有在您的 collectionViewLayout 中给它一个 UICollectionViewFlowLayout:

于 2013-04-10T04:24:23.620 回答