我在viewDidLoad中添加了一个像这样的集合视图...
self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 10, 10) collectionViewLayout:flowLayout];
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
self.collectionView.backgroundColor = [UIColor whiteColor];
[self.collectionView registerClass:[CameraCell class] forCellWithReuseIdentifier:CameraCellReuseIdentifier];
[self.collectionView setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.view addSubview:self.collectionView];
我有一个名为CameraCell的UICollectionViewCell子类,带有这样的init ......
- (id)init
{
self = [super init];
if (self) {
// Add customisation here...
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(imageChanged:) name:@"image" object:nil];
self.contentView.backgroundColor = [UIColor yellowColor];
self.imageView = [[UIImageView alloc] initWithFrame:CGRectZero];
self.imageView.contentMode = UIViewContentModeScaleAspectFill;
self.imageView.clipsToBounds = YES;
[self.imageView setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.contentView addSubview:self.imageView];
NSDictionary *views = NSDictionaryOfVariableBindings(_imageView);
[self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_imageView]|"
options:0
metrics:nil
views:views]];
[self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[_imageView]|"
options:0
metrics:nil
views:views]];
}
return self;
}
但是当我运行应用程序时,集合视图就在那里,我可以滚动它但我看不到任何单元格。我在单元格的init中添加了一个断点,它永远不会被调用。还有其他方法我必须覆盖吗?
修改
当我在cellForItemAtIndexPath中记录单元格时......
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CameraCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CameraCellReuseIdentifier forIndexPath:indexPath];
NSLog(@"%@", cell);
return cell;
}
显示正确的班级......
<CameraCell: 0x1f07ba30; baseClass = UICollectionViewCell; frame = (0 20; 320 280); layer = <CALayer: 0x1f07bb40>>
答案 0 :(得分:41)
您需要实施的方法是- (instancetype)initWithFrame:(CGRect)rect
答案 1 :(得分:27)
我最近在iOS7 SDK中尝试过此操作,因为initWithCoder
从未被调用过,所以我必须覆盖initWithFrame
。
答案 2 :(得分:12)
如果从StoryBoard加载单元格,那么您将要覆盖这样的初始化程序:
<强>夫特强>
override init?(coder aDecoder: NSCoder) {
super.init(coder:aDecoder)
//You Code here
}
<强>目标C 强>
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
//You code here
}
如果从nib加载,则需要覆盖这样的初始值设定项:
<强>夫特强>
override init(frame: CGRect) {
super.init(frame:frame)
//You Code here
}
<强>目标C 强>
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
//You code here
}
答案 3 :(得分:0)
我知道这个问题很老了,但我个人很懒,想从某个地方复制粘贴代码。不幸的是,上面没有 100% 正确的解决方案,所以我写了通用模式(对于基于 IB 和基于代码的单元格)
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
commonInit()
}
override func awakeFromNib() {
commonInit()
}
private func commonInit() {
}
答案 4 :(得分:-1)
在我的情况下,当我使用[[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 10, 10) collectionViewLayout:flowLayout]
以编程方式实例化collectionView时,initWithFrame
永远不会被调用,但是initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout
已经调用。