-1

我无法回忆保存在目录中并显示在 UICollectionView 上的图像。有没有可能我遗漏了什么?我添加了日志,这样我就可以看到正在发生的事情,并且 i 和 j 都出现了,但是崩溃说

terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<HatsCell 0x208940c0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key image.'
*** First throw call stack:
(0x3467c2a3 0x3c36097f 0x3467bf99 0x34ee91d9 0x366282b9 0x34ee4f2b 0x3460261b 0x3662131d 0x3689f077 0x3689f56b 0xa0911 0x3689a02d 0x3689af25 0x3689c7bb 0x36487803 0x36231d8b 0x36231929 0x3623285d 0x36232243 0x36232051 0x36231eb1 0x346516cd 0x3464f9c1 0x3464fd17 0x345c2ebd 0x345c2d49 0x3818a2eb 0x364d8301 0x8e7cd 0x3c797b20)
libc++abi.dylib: terminate called throwing an exception

这是我的召回代码。

 - (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    allImagesArray = [[NSMutableArray alloc] init];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *location=@"hats";
    NSString *fPath = [documentsDirectory stringByAppendingPathComponent:location];
    NSArray *directoryContent = [[NSFileManager defaultManager] directoryContentsAtPath: fPath];
    collectionHats.delegate =self;
    collectionHats.dataSource=self;
    for(NSString *str in directoryContent){
        NSLog(@"i");
        NSString *finalFilePath = [fPath stringByAppendingPathComponent:str];
        NSData *data = [NSData dataWithContentsOfFile:finalFilePath];
        if(data)
        {
            UIImage *image = [UIImage imageWithData:data];
            [allImagesArray addObject:image];
        }}}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    NSLog(@"j");
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [allImagesArray count];

}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuseID = @"ReuseID";
    HatsCell *mycell = (HatsCell *) [collectionView dequeueReusableCellWithReuseIdentifier:reuseID forIndexPath:indexPath];
    return mycell;
}

这是给我在帽子单元格中的 .h

import <UIKit/UIKit.h>

@interface HatsCell : UICollectionViewCell
@property (weak, nonatomic) IBOutlet UIImageView *imageInCell;

@end
4

1 回答 1

0

您尝试设置实例image的某处。没有这个属性,也没有添加它,所以你得到一个例外。您应该设置单元格的图像。HatsCellUICollectionViewCellHatsCellimageInCell


当你得到单元格时:

HatsCell *mycell = (HatsCell *) [collectionView dequeueReusableCellWithReuseIdentifier:reuseID forIndexPath:indexPath];

然后你需要更新它:

mycell.imageInCell.image = [allImagesArray objectAtIndex:indexPath.row];
于 2013-08-18T11:24:41.673 回答