[UICollectionViewCell cellImageView]:发送到实例的无法识别的选择器

时间:2013-01-29 21:51:53

标签: ios uiimageview uiimage selector uicollectionviewcell

在UICollectionView上的

我想用图像填充CollectionViewCell。但是我得到以下错误:

我的错误在哪里?我该如何解决?

2013-01-29 22:25:38.606 foobar[1277:c07] CollectionViewController loaded with searches: 22
2013-01-29 22:25:38.609 foobar[1277:c07] id: 0 -> content: <UIImage: 0xaa618e0>
2013-01-29 22:25:38.610 foobar[1277:c07] -[UICollectionViewCell cellImageView]: unrecognized selector sent to instance 0x939a7b0
2013-01-29 22:25:38.610 foobar[1277:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UICollectionViewCell cellImageView]: unrecognized selector sent to instance 0x939a7b0'
*** First throw call stack:
(0x1d9f012 0x139fe7e 0x1e2a4bd 0x1d8ebbc 0x1d8e94e 0x478a 0x7e9a1a 0x7eb034 0x7ed2d1 0x33792d 0x13b36b0 0x2afffc0 0x2af433c 0x2af4150 0x2a720bc 0x2a73227 0x2a738e2 0x1d67afe 0x1d67a3d 0x1d457c2 0x1d44f44 0x1d44e1b 0x1cf97e3 0x1cf9668 0x2e765c 0x24dd 0x2405)
libc++abi.dylib: terminate called throwing an exception
(lldb) 

导致错误的代码如下:

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    CollectionViewCell *myCell = (CollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:Cellid forIndexPath:indexPath];

    int row = [indexPath row];

    NSLog(@"id: %d -> content: %@", row, [self.searches objectAtIndex:row]);

    myCell.cellImageView.image = self.searches[row]; // <-- Signal SIGABRT
    return myCell;
}

UICollectionViewCell的代码如下:

CollectionViewCell.h

#import <UIKit/UIKit.h>

@interface CollectionViewCell : UICollectionViewCell
@property (strong, nonatomic) IBOutlet UIImageView *cellImageView;

@end

CollectionViewCell.m

#import "CollectionViewCell.h"

@implementation CollectionViewCell

@synthesize cellImageView;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

@end

3 个答案:

答案 0 :(得分:8)

请确保在Interface Builder中为单元格设置自定义类CollectionViewCell

答案 1 :(得分:1)

我忘了将类CollectonViewCell添加到Storyboard中的Collection View Cell(Identity Insepector)。在那之后重新分配IBOutlet cellImageView它有效!

答案 2 :(得分:0)

当您致电[collectionView dequeueReusableCellWithReuseIdentifier:Cellid forIndexPath:indexPath]时,您不会返回属于自定义集合视图单元子类的单元格,而是标准UICollectionViewCell。这告诉我,在您使用容器视图注册单元格时可能会出现问题。如果您发布该代码,它可能会有所帮助。