gpt4 book ai didi

ios - 自定义 Collection View 单元格属性在设置时抛出父类(super class)的错误

转载 作者:行者123 更新时间:2023-11-29 03:50:53 25 4
gpt4 key购买 nike

我有一个 View Controller ,充当 UICollectionViewdataSourcedelegate

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath {
PhotoCell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"PIC_CELL" forIndexPath:indexPath];
UIImage *photo = [UIImage imageWithData:[[_fetchedImages objectAtIndex:indexPath.row] valueForKey:@"picture"]];
if (photo) {
cell.photo = photo;
}
return cell;
}

在上面的方法中,我实例化了一个自定义 Collection View 单元格并尝试设置它的 photo 属性。

PhotoCell.h

@interface PhotoCell : UICollectionViewCell {
}

@property (nonatomic, strong) IBOutlet UIImageView *imageView;
@property (nonatomic, strong) UIImage *photo;

@end

PhotoCell.m

@implementation PhotoCell

- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
//customization
}
return self;
}

- (void)setPhoto:(UIImage *)photo {
UIGraphicsBeginImageContextWithOptions(CGSizeMake(83.0, 83.0), NO, 0.0);
[photo drawInRect:CGRectMake(0, 0, 83, 83)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
if (_photo != newImage)
_photo = newImage;

self.imageView.image = _photo;
}

在这里,我重写了 photo 属性的 setter ,允许在将传递的照片设置为属性之前调整其大小。

但是,当代码执行并实例化 PhotoCell 自定义单元格时,尝试在 -cellForItemAtIndexPath 中设置 photo 属性时会引发以下错误:方法:

    -[UICollectionViewCell setPhoto:]: unrecognized selector sent to instance 0x155c1270
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UICollectionViewCell setPhoto:]: unrecognized selector sent to instance 0x155c1270'

系统似乎将自定义单元格视为其父类(super class) UICollectionViewCell 的实例,而不是实际类 PhotoCell 的实例。当不重写 setter 时,也会引发相同的错误。

为什么自定义单元格被视为其父类(super class)的实例,导致setter无法识别?

最佳答案

该问题与您注册 PIC_CELL 的方式有关。无论在哪里完成,都会指定错误的类。这可能位于 XIB 文件或您的代码中。

关于ios - 自定义 Collection View 单元格属性在设置时抛出父类(super class)的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17076170/

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