![](/img/trans.png)
[英]How to implement Custom UICollectionViewCell using Swift in iOS
[英]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.