简体   繁体   English

如何在iOS中使用Storyboard创建自定义UICollectionViewCell?

[英]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 ,请执行以下操作:

  • Create a subclass of UITableViewCell 创建UITableViewCell的子类
  • Set this subclass as the class for the UITableViewCell in the storyboard 将此子类设置为情节UITableViewCell中的UITableViewCell的类
  • Set a reuse identifier. 设置重用标识符。
  • Import this class in my view controller implementation, declare a cell of this subclass and proceed. 将该类导入我的视图控制器实现中,声明此子类的单元格并继续。

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.

相关问题 如何在iOS中使用Swift实现自定义UICollectionViewCell - How to implement Custom UICollectionViewCell using Swift in iOS 使用Storyboard和ViewController创建iOS自定义框架 - Create iOS custom framework using storyboard and viewcontroller 如何使用iOS在故事板上创建ImageView中心? - How to create ImageView center on storyboard using iOS? 无法在 Storyboard 中更改自定义 UICollectionViewCell - Cannot change custom UICollectionViewCell in Storyboard 如何在故事板上创建一个Segue,从编程注册的UICollectionViewCell到新的UIViewController? - How to create a Segue on the Storyboard, from a programmatically registered UICollectionViewCell to a new UIViewController? 如何忽略情节提要上的UICollectionViewCell大小? - how to ignore UICollectionViewCell size on storyboard? 如何创建具有弯曲侧面的自定义uicollectionviewcell? - How to create a custom uicollectionviewcell with a curved side? 如何在情节提要中创建弹出窗口-iOS - How to create popup in storyboard - iOS Xamarin iOS:从情节提要中的UICollectionViewCell获取信息 - Xamarin iOS: Get information from a UICollectionViewCell in the storyboard 如何使用iOS故事板在UIPageviewController内部创建多个UIView? - How to create multiple UIViews Inside the UIPageviewController using iOS storyboard?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM