Fogmeister
Fogmeister

Reputation: 77661

UICollectionViewCell subclass init never run

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

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...

- (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?

EDIT

When I log the cell in 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>> 

Upvotes: 32

Views: 36913

Answers (6)

Joe
Joe

Reputation: 3931

Swift 5:

required init?(coder aDecoder: NSCoder) {
    super.init(coder:aDecoder)
    // Your code here.
}

Upvotes: -2

iago849
iago849

Reputation: 1854

I know the question is way old, but personally I was lazy and wanted to copy-paste code from somewhere. Unfortunately, there is no 100% correct solution above so I wrote common pattern (for both IB-based and code-based cells)

    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() {
        
    }

Upvotes: 2

Alejandro
Alejandro

Reputation: 3746

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

Upvotes: 48

Barlow Tucker
Barlow Tucker

Reputation: 6529

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

Swift

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

Objective-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

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

Upvotes: 24

Jakehao
Jakehao

Reputation: 1019

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

Upvotes: -1

Archit Baweja
Archit Baweja

Reputation: 983

I recently tried this in the iOS7 SDK, and I had to override initWithCoder because initWithFrame was never called.

Upvotes: 28

Related Questions