繁体   English   中英

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

[英]How to create custom UICollectionViewCell using Storyboard in iOS?

我有一个UIViewController中,我有一个UICollectionView中的.The细胞UICollectionView必须是图像,所以我明白,我需要继承UICollectionViewCell类。 因此,基本上,我需要一个UIViewController,该UIViewController的UICollectionView带有一个自定义UICollectionViewCell。 我正在使用情节提要,并且在需要创建自定义UITableVieCell ,请执行以下操作:

  • 创建UITableViewCell的子类
  • 将此子类设置为情节UITableViewCell中的UITableViewCell的类
  • 设置重用标识符。
  • 将该类导入我的视图控制器实现中,声明此子类的单元格并继续。

但是当我对UICollectionViewCell遵循相同的UICollectionViewCell例如。 我创建一个名为CustomCollectionViewCell的子类,在cellForItemAtIndexPath声明它

CustomCollectionViewCell *cell = (CustomCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];

NSLog(@"%@",cell);

这会记录<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类,因此无法添加图像。

我查看了其他StackOverflow答案,并阅读了答案,说不这样做:

[self.collectionView registerClass:[CustomCollectionViewCell class] forCellWithReuseIdentifier:@"Cell"];

如果我使用情节提要,那么我将其注释掉。

所有重用标识符,出口等均已设置。 我似乎找不到错误所在。

任何帮助表示赞赏。

尝试这个,

Identity Inspector设置您的自定义collectionview单元格类,例如

在此处输入图片说明

并在cellForItemAtIndexPath

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    CollectionViewCell *cell = (CollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    NSLog(@"%@", cell);
    return cell;
}

并获得日志为

<CollectionViewCell: 0x7af70000; baseClass = UICollectionViewCell; frame = (0 0; 70 69); clipsToBounds = YES; opaque = NO; layer = <CALayer: 0x7af70210>>

我想到了。

这是一个愚蠢的错误。 我正在使用的重用标识符已被其他地方的其他单元使用。 对其进行了更改,现在可以完美运行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM