UICollectionViewCell子类init永远不会运行

Fog*_*ter 27 ios uicollectionview uicollectionviewcell

我在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];
Run Code Online (Sandbox Code Playgroud)

我有一个名为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;
}
Run Code Online (Sandbox Code Playgroud)

但是当我运行应用程序时,集合视图就在那里,我可以滚动它,但我看不到任何单元格.我在单元格的init中添加了一个断点,它永远不会被调用.还有其他方法我必须覆盖吗?

编辑

当我在cellForItemAtIndexPath中记录单元格时......

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CameraCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CameraCellReuseIdentifier forIndexPath:indexPath];

    NSLog(@"%@", cell);

    return cell;
}
Run Code Online (Sandbox Code Playgroud)

它显示正确的类...

<CameraCell: 0x1f07ba30; baseClass = UICollectionViewCell; frame = (0 20; 320 280); layer = <CALayer: 0x1f07bb40>> 
Run Code Online (Sandbox Code Playgroud)

Ale*_*dro 44

您需要实现的方法是 - (instancetype)initWithFrame:(CGRect)rect

  • 你得到的框架参数有点无用,但你仍然需要在这里初始化你自己的属性. (3认同)

Arc*_*eja 27

我最近在iOS7 SDK中试过这个,我不得不覆盖,initWithCoder因为initWithFrame从未调用过.

  • 那那就是原因.`initWithCoder`是从故事板构建的视图的指定初始化程序.`initWithFrame`用于以编程方式构建的视图. (36认同)
  • 频繁的问题:)我通常将所有构造放在`init`中,然后从重写的`initWithFrame`和`initWithCoder`中调用它.它使细胞类更普遍.或者,当我需要保存原始的initWithCoder行为时,我创建另一个方法`universalInit`,将所有自定义代码放在那里并从所有默认初始值设定项中调用它.当然,使用Swift这个技巧不再方便. (2认同)

Bar*_*ker 16

如果从StoryBoard加载单元格,那么您将希望覆盖这样的初始化程序:

迅速

override init?(coder aDecoder: NSCoder) {
  super.init(coder:aDecoder)
  //You Code here
} 
Run Code Online (Sandbox Code Playgroud)

Objective-C的

- (id)initWithCoder:(NSCoder *)aDecoder {
  self = [super initWithCoder:aDecoder];
  //You code here
}
Run Code Online (Sandbox Code Playgroud)

如果你从一个笔尖加载,那么你会想要覆盖这样的初始化器:

迅速

override init(frame: CGRect) {
  super.init(frame:frame)
  //You Code here
} 
Run Code Online (Sandbox Code Playgroud)

Objective-C的

- (id)initWithFrame:(CGRect)frame {
  self = [super initWithFrame:frame];
  //You code here
}
Run Code Online (Sandbox Code Playgroud)