2

在 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
4

3 回答 3

8

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

于 2013-01-29T22:16:18.080 回答
1

我忘了添加类:CollectonViewCell到故事板(身份检查器)中的集合视图单元格。之后并重新分配IBOutlet cellImageView它的工作!

于 2013-01-29T22:17:34.580 回答
0

当您调用时,[collectionView dequeueReusableCellWithReuseIdentifier:Cellid forIndexPath:indexPath]您不会被返回一个属于您的自定义集合视图单元子类的单元格,而是一个标准的UICollectionViewCell. 这向我表明,在您将单元格注册到容器视图时,可能出现了问题。如果您也发布该代码,它可能会有所帮助。

于 2013-01-29T22:13:43.503 回答