ios - UICollectionViewCell 子类 init 从不运行

标签 ios uicollectionview uicollectionviewcell

我像这样在 viewDidLoad 中添加了一个 Collection View ......

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 子类,带有这样的初始化...

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

但是当我运行该应用程序时, Collection View 就在那里,我可以滚动它,但我看不到任何单元格。我在单元格的 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>> 

最佳答案

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

关于ios - UICollectionViewCell 子类 init 从不运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16531867/

相关文章:

iphone - 让阅读器应用程序全屏显示?

ios - 应用程序购买的推荐链接

ios - NSURLConnection(作为 AFNetworking 的一部分)不调用 NSURLConnectionDataDelegate 委托(delegate)

swift - UICollectionViewCell 不强制单元格大小

ios - 如何滚动另一个 UICollectionView 下方的 UICollectionView?

ios - UICollectionView 刷新问题

ios - 将 CocoaPods 与 2 个项目一起使用时,运行时输出 objc[69590]

ios - 计算tableView的heightForRowAtIndexPath中的UICollectionView contentSize

ios - TableView 不返回数据

ios - 显示从 url 到 Collection View Swift 单元格的 3 张图像