gpt4 book ai didi

ios - UICollectionViewCell 子类 init 从不运行

转载 作者:IT王子 更新时间:2023-10-29 07:53:25 25 4
gpt4 key购买 nike

我像这样在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com