[英]How to create custom UICollectionViewCell using Storyboard in iOS?
I have a UIViewController
in which I have a UICollectionView
.The cells of the UICollectionView
need to be images so I understand that I need to subclass the UICollectionViewCell
class. 我有一个UIViewController
中,我有一个UICollectionView
中的.The细胞UICollectionView
必须是图像,所以我明白,我需要继承UICollectionViewCell
类。 So basically, I need a UIViewController which has a UICollectionView that has a custom UICollectionViewCell. 因此,基本上,我需要一个UIViewController,该UIViewController的UICollectionView带有一个自定义UICollectionViewCell。 I am using a storyboard and when I need to create a custom UITableVieCell
, I do the following: 我正在使用情节提要,并且在需要创建自定义UITableVieCell
,请执行以下操作:
UITableViewCell
创建UITableViewCell
的子类 UITableViewCell
in the storyboard 将此子类设置为情节UITableViewCell
中的UITableViewCell
的类 But when I follow the same for a UICollectionViewCell
, for eg. 但是当我对UICollectionViewCell
遵循相同的UICollectionViewCell
, 例如。 I create a subclass called CustomCollectionViewCell
, and declare it in the cellForItemAtIndexPath
like 我创建一个名为CustomCollectionViewCell
的子类,在cellForItemAtIndexPath
声明它
CustomCollectionViewCell *cell = (CustomCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
NSLog(@"%@",cell);
This logs <UICollectionViewCell: 0x137529010; frame = (10 10; 300 164); layer = <CALayer: 0x170220880>>
这会记录<UICollectionViewCell: 0x137529010; frame = (10 10; 300 164); layer = <CALayer: 0x170220880>>
<UICollectionViewCell: 0x137529010; frame = (10 10; 300 164); layer = <CALayer: 0x170220880>>
<UICollectionViewCell: 0x137529010; frame = (10 10; 300 164); layer = <CALayer: 0x170220880>>
no matter what I do. <UICollectionViewCell: 0x137529010; frame = (10 10; 300 164); layer = <CALayer: 0x170220880>>
不管我做什么。 I can just not initialize my custom UICollectionViewCell
class and hence cannot add the images. 我无法初始化我的自定义UICollectionViewCell
类,因此无法添加图像。
I looked at other StackOverflow answers and read answers saying not to do this: 我查看了其他StackOverflow答案,并阅读了答案,说不这样做:
[self.collectionView registerClass:[CustomCollectionViewCell class] forCellWithReuseIdentifier:@"Cell"];
if I'm using storyboard, so I commented it out. 如果我使用情节提要,那么我将其注释掉。
All the reuse identifiers, outlets etc have been set. 所有重用标识符,出口等均已设置。 I can't seem to find where the error is. 我似乎找不到错误所在。
Any help is appreciated. 任何帮助表示赞赏。
Try this, 尝试这个,
set your custom collectionview cell class in Identity Inspector
like 在Identity Inspector
设置您的自定义collectionview单元格类,例如
and in cellForItemAtIndexPath
并在cellForItemAtIndexPath
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
CollectionViewCell *cell = (CollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
NSLog(@"%@", cell);
return cell;
}
and got log as 并获得日志为
<CollectionViewCell: 0x7af70000; baseClass = UICollectionViewCell; frame = (0 0; 70 69); clipsToBounds = YES; opaque = NO; layer = <CALayer: 0x7af70210>>
I figured it out. 我想到了。
It was a stupid mistake. 这是一个愚蠢的错误。 The reuse identifier that I was using was already being used by a different cell someplace else. 我正在使用的重用标识符已被其他地方的其他单元使用。 Changed it and it works perfectly now. 对其进行了更改,现在可以完美运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.