Вопрос

I have created a UICollectionViewCell that looks like this

#import "PhotoCell.h"


@implementation PhotoCell

@synthesize imageView;

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.backgroundColor = [UIColor clearColor];

        imageView = [[UIImageView alloc] initWithFrame:(CGRect){.origin = CGPointMake(4.0f, 4.0f), .size=CGRectInset(frame, 4.0f, 4.0f).size}];
        imageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        [self.contentView addSubview:imageView];
    }
    return self;
}

@end

I am calling this from one of my view controllers where I am trying to set up a UICollectionView.. this is where I am calling this class

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath;
{
    PhotoCell *cell = (PhotoCell *)[self.photoCollectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];

    NSDictionary *currentPhotoDict = [imageArray objectAtIndex:indexPath.row];
    UIImage *imageForCollection = [UIImage imageWithData:[currentPhotoDict objectForKey:@"DImage"]];

    // but I have no idea where to pass the imag....

    cell.imageView.image = imageForCollection;

    return cell;
}

The issue is, I am getting this error then my app falls over in a heap.

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

Update

I have created a .nib file call aPhotoCell and I have deleted the UIView from it and added a UIImageView. I have then changed the objects Custom Class to PhotoCell and now that I can see the IBOutlet of UIImageView from PhotoCell I have hooked that up, but still I am receiving the same error. From the comments and answers below, this is what I thought I had to do to fix.

Это было полезно?

Решение

Couple of things..

In your PhotoCell class - declare a public property:

 @property (weak, nonatomic) IBOutlet UIImageView *imageView;

Then in your PhotoCell xib (I assume this is where you're creating it from?) connect it to the IBOutLet you created.

I think you're doing something wrong in your cellForItemAtIndexPath - as your error shows an instance of UICollectionViewCell doesn't respond to cell.imageView.image = imageForCollection;

Make sure your cell identifier is also the same as you put it in your storyboard / xib file Rewrite your cellForItemAtIndexPath method like this:

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath;{
PhotoCell *cell = [ self.photoCollectionView dequeueReusableCellWithReuseIdentifier:@"CORRECT CELL IDENTIFIER HERE" forIndexPath:indexPath];

NSDictionary *currentPhotoDict = [imageArray objectAtIndex:indexPath.row];
UIImage *imageForCollection = [UIImage imageWithData:[currentPhotoDict objectForKey:@"DImage"]];


cell.imageView.image = imageForCollection;

return cell;
}

Edit

It seems like you created a subclass of UICollectionViewCell and never created a xib file for it. Since you're not using storyboards do this:

In Xcode:

Create a new file - select "User Interface" from the left under 'iOS' and select "empty" from the selection it shows you.

After creating your xib file - go to it and delete UIView that is there. Then from the object library on your right, look for UICollectionView Cell and drag it onto the view and then drag a UIImageView and place it in your UICollectionView cell.

Now select UICollectionViewCell you just created and set its class to the PhotoCell class. Connect your IBOutlets like above. Make sure to set the 'Reusable Cell' attribute too. Its important here.

Then in the viewController that loads your UICollectionView - add this to viewDidLoad

        [photoCollectionView registerNib:[UINib nibWithNibName:@"THE XIB FILE NAME YOU JUST CREATED eg: PhotoCell" bundle:nil] forCellWithReuseIdentifier:@"REUSE IDENTIFIER HERE"];

This should help you.

Другие советы

I also had this problem and my issue was that I had an error in my code in another place.

I used this code to register the the UICollectionViewCell in the viewDidLoad method.

[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"reuseLabel"];

However, when I tried to update the cell with my custom UICollectionViewCell I didn't change this line to work my custom cell. So I changed the code to this:

[self.collectionView registerClass:[MyCustomCell class] forCellWithReuseIdentifier:@"reuseLabel"];
  1. According to UICollectionView Class Reference, CollectionViewCell just didn't have imageView property. You should add it to cell's contentView in Storyboard / in code first.

  2. Also, You're using own PhotoCell class. Check that your cell have correct class name in Storyboard.

I assume the cell identified by "cell" is defined in a Xib file. If so, I suspect that the error is because the main view in the Xib file is defined with the UICollectionViewCell class instead of your custom PhotoCell class.

EDIT:
After learning that you're not using xib or storyboards, what happens is that dequeueReusableCellWithReuseIdentifier doesn't find any cell with the given identifier "cell" becouse you haven't defined it anywhere. From the documentation:

You must register a class or nib file using the registerNib:forCellReuseIdentifier: or registerClass:forCellReuseIdentifier: method before calling this method.

In your case you should use registerClass:... to register your class with the "cell" or any other identifier.

PhotoCell xib file need set "cell" in Identifier Your code should be:

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath;
{
    PhotoCell *cell = (PhotoCell *)[self.photoCollectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    if (cell == nil) {
      // Load the top-level objects from the custom cell XIB.
    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"cell" owner:self options:nil];
    // Grab a pointer to the first object (presumably the custom cell, as that's all the XIB should contain).
    cell = [topLevelObjects objectAtIndex:0];
    }
    NSDictionary *currentPhotoDict = [imageArray objectAtIndex:indexPath.row];
    UIImage *imageForCollection = [UIImage imageWithData:[currentPhotoDict objectForKey:@"DImage"]];

    // but I have no idea where to pass the imag....

    cell.imageView.image = imageForCollection;

    return cell;

}

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top