我在UICollectionView
中有一个UIViewController
,我在self.view
方法中分配,初始化并添加到viewDidLoad
。我在集合视图中使用了两种类型的单元格,一种是子类型的单元格,另一种是标准UICollectionViewCell
。现在一切正常,按照预期。仅此而已,registerClass:forCellWithReuseIdentifier:
在cellForItemAtIndexPath:
方法内,dequeueReusableCellWithReuseIdentifier:forIndexPath:
之前调用registerClass:forCellWithReuseIdentifier:
。这意味着viewDidLoad
被称为每个时间一个单元格出列。对我来说似乎多余。
所以我尝试将register-class代码移动到viewDidLoad
方法本身,就在初始化collection-view之后。使用崩溃的结果必须为标识符注册nib或类,或者在故事板中连接原型单元。然后我尝试将代码移到self.view
中的viewWillAppear:
之后将代码移动到 end ,仍然是同样的崩溃。然后我将代码移动到registerClass:forCellWithReuseIdentifier:
,然后再次崩溃。
cellForItemAtIndexPath:
似乎只能从XIBs
方法内部调用,而不是其他地方。
我在这里做错了吗?或者这只是如何为集合视图注册单元格?
注意:没有viewDidLoad
或故事板。这一切都是通过编码完成的。也是MRC,而不是ARC。
编辑:添加了代码
内部photosCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 3, 320, height)
collectionViewLayout:[[[UICollectionViewFlowLayout alloc] init] autorelease]];
photosCollectionView.opaque = YES;
photosCollectionView.layer.opaque = YES;
photosCollectionView.clearsContextBeforeDrawing = NO;
photosCollectionView.backgroundColor = UIColorFromRGB(0xd3d7dc);
photosCollectionView.layer.backgroundColor = UIColorFromRGB(0xd3d7dc).CGColor;
photosCollectionView.dataSource = self;
photosCollectionView.delegate = self;
photosCollectionView.contentInset = UIEdgeInsetsMake(0, 0, 49, 0);
//other UI element init
[self.view addSubview:photosCollectionView];
[photosCollectionView release];
[photosCollectionView registerClass:[collectionCell class] forCellWithReuseIdentifier:@"AllPhotosCell"];//subclassed cell
:
cellForItemAtIndexPath:
在NSString* CellIdentifier = @"AllPhotosCell";
collectionCell *cell = [photosCollectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier
forIndexPath:indexPath];
(相关代码)中:
{{1}}