ios UICollectionViewCell 子类 init 永远不会运行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16531867/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 23:34:01  来源:igfitidea点击:

UICollectionViewCell subclass init never run

iosuicollectionviewuicollectionviewcell

提问by Fogmeister

I have added a collection view in viewDidLoad like this...

我在 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];

And I have a UICollectionViewCell subclass called CameraCell with an init like this...

我有一个名为 CameraCell 的 UICollectionViewCell 子类,它的初始化是这样的......

- (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;
}

But when I run the app the collection view is there and I can scroll it but I can't see any cells. I have added a breakpoint in the cell's init and it never gets called. Is there another method I have to override?

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

EDIT

编辑

When I log the cell in cellForItemAtIndexPath...

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

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

    NSLog(@"%@", cell);

    return cell;
}

It displays the correct class...

它显示正确的类...

<CameraCell: 0x1f07ba30; baseClass = UICollectionViewCell; frame = (0 20; 320 280); layer = <CALayer: 0x1f07bb40>> 

回答by Alejandro

The method you need to implement is - (instancetype)initWithFrame:(CGRect)rect

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

回答by Archit Baweja

I recently tried this in the iOS7 SDK, and I had to override initWithCoderbecause initWithFramewas never called.

我最近在 iOS7 SDK 中尝试了这个,我不得不重写,initWithCoder因为initWithFrame从来没有被调用过。

回答by Barlow Tucker

If the cell is loaded from a StoryBoard then you will want to override the initializers like this:

如果单元格是从 StoryBoard 加载的,那么您将需要像这样覆盖初始值设定项:

Swift

迅速

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

Objective-C

目标-C

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

If you are loading from a nib, then you will want to override the initializers like this:

如果您从笔尖加载,那么您将需要像这样覆盖初始值设定项:

Swift

迅速

override init(frame: CGRect) {
  super.init(frame:frame)
  //You Code here
} 

Objective-C

目标-C

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

回答by Jakehao

In my case, when I am using [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 10, 10) collectionViewLayout:flowLayout]to programatically instantiate a collectionView, initWithFramenever get called, but initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layoutdid.

就我而言,当我使用[[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 10, 10) collectionViewLayout:flowLayout]以编程方式实例化 collectionView 时,initWithFrame永远不会被调用,但initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout确实如此。