Collection View Cell does not display image in ima

2019-08-07 03:06发布

问题:

I am trying to create a simple gallery for an iOS application. I'm working with a UICollectionView inside of a ViewController. Everything seems to be working fine, but I can't get the image I'm trying to use to display inside of the collection view cell. I will just dump some code here, I have been desperate to find a solution but nothing seems to work.

This is the gallery.m file:

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView;
{
    //just a random value for testing
    return 2;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:   (NSInteger)section
{
    //another random test value
    return 5;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"customCell";

    customCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier
                                          forIndexPath:indexPath];
    //UIImage *image = [dataArray objectAtIndex:indexPath.row];
    cell.imageView.image = [UIImage imageNamed:@"skull.png"];

    //I tried all of the below code to get the image to show but nothing seemed to work
    //[cell.imageView setImage:[UIImage imageNamed:@"skull.png"]];
    //[cell.imageView setNeedsDisplay];
    //[cell.imageView reloadInputViews];
    return cell;
}

This is the customCell.h file (the UIImageView outlet is connected to the image view inside of the collection view cell):

#import <UIKit/UIKit.h>

@interface customCell : UICollectionViewCell
@property (strong, nonatomic) IBOutlet UIImageView *imageView;

@end

Concerning the connections in the storyboard everything should be fine, also it would be difficult to show here but I will try to explain anyways. The UICollectionView is connected in the gallery.h file like so:

@interface gallery : UIViewController <UICollectionViewDataSource, UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

@property (strong, nonatomic) IBOutlet UICollectionView *collectionView;


@end

The collection view cell in the storyboard has the custom class "customCell" and identifier "customCell".

If anybody could point me to where my mistake could be it would be awesome! Thank you!

回答1:

Look in the storyboard, sometimes in cells, xCode set images frame to (0,0,0,0). It made me crazy a lot of times ^^

EDIT : To create a custom collection cell, create an empty XIB file. Add a collectionViewCell in it with all you want. Make the connections with you .h file.

In your viewController -> viewDidLoad method add

[myCollectionView registerNib:[UINib nibWithNibName:@"collectionCellXIB" bundle:nil] forCellWithReuseIdentifier:myIdentifier];

And in cellForRowAtIndexPath

customCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:myIdentifier forIndexPath:indexPath];

if(!cell)
{
    cell = [collectionView dequeueReusableCellWithReuseIdentifier:myIdentifier forIndexPath:indexPath];
}


回答2:

I had this very similar problem. I could not display image view inside uicollection view cell, when storyboard is used. I just removed the following code in ViewDidLoad of CollectionViewController:

//[self.collectionView registerClass:[CustomCollectionViewCell class]forCellWithReuseIdentifier:reuseIdentifier];

Now the ImageView is displayed inside cell.