Murat Calim
Murat Calim

Reputation: 580

UICollectionView doesn't show cell images

I have a viewcontroller and uicollectionview in it.

I used following code, but there is no visible images in uicollectionview, just yellow boxes :

myviewcontroller.h

{
IBOutlet UICollectionView *gallery1;
//IBOutlet UIImage *inimage;
NSArray *recipePhotos;
}

@property (nonatomic, retain) IBOutlet UICollectionView *gallery1;
//@property (nonatomic, retain) IBOutlet UIImage *inimage;

and myviewcontroller.m

- (void)viewDidLoad
{
[super viewDidLoad];

recipePhotos = [NSArray arrayWithObjects:@"im1.png","im2.png", "im3.png", "im4.png", "im5.png", "im6.png",  nil];

UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
[flowLayout setItemSize:CGSizeMake(100, 100)];
[flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
[self.gallery1 setCollectionViewLayout:flowLayout];
[self.gallery1 setAllowsSelection:YES];
self.gallery1.delegate=self;
}

- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection: (NSInteger)section {
return recipePhotos.count;
}

- (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView {
return 1;
}

// cellforitematindexpath

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

[gallery1 registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:[NSString stringWithFormat:@"c%d",indexPath.row]];
UICollectionViewCell *cell = [gallery1 dequeueReusableCellWithReuseIdentifier:[NSString stringWithFormat:@"c%d",indexPath.row] forIndexPath:indexPath];

cell.backgroundColor = [UIColor yellowColor];
UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
recipeImageView.image = [UIImage imageNamed:[recipePhotos objectAtIndex:indexPath.row]];
[cell addSubview:recipeImageView];
return cell;
}

I cannot see any image (image file are existing) in cells in uicollectionview.

Thanks for any help.

Upvotes: 3

Views: 11028

Answers (4)

ingconti
ingconti

Reputation: 11666

be careful:

doing:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
    UIImageView *imgView = [[UIImageView **alloc**] initWithFrame:CGRectMake(0,0,100,100)];
    imgView.contentMode = UIViewContentModeScaleAspectFit;
    imgView.clipsToBounds = YES;
    imgView.image = [self.imageArray objectAtIndex:indexPath.row];
    [cell **addSubview:imgView**];
    return cell;
}

You are allocating an image (and it can be ok.. even if a bit expensive..) BUT ADDING every time, so using a cell 100 times will add 100 subviews.

Upvotes: 0

leokash
leokash

Reputation: 200

here is my take:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,100,100)];
    imgView.contentMode = UIViewContentModeScaleAspectFit;
    imgView.clipsToBounds = YES;
    imgView.image = [self.imageArray objectAtIndex:indexPath.row];
    [cell addSubview:imgView];
    return cell;
}

The imageView should have a frame... the best option is to create your own frame. also make sure that the images will fit the frame ratio and clip to the bounds!

Upvotes: 5

Matt Mc
Matt Mc

Reputation: 9297

@Adam is right, don't call registerClass every time you make a new cell. However...

Something is wrong here:

UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
recipeImageView.image = [UIImage imageNamed:[recipePhotos objectAtIndex:indexPath.row]];
[cell addSubview:recipeImageView];

You are asking the cell for a subview, then setting an image for that subview, and then attempting to add that subview back into the hierarchy...where it already is?

Does that subview even exist? When is it created? Perhaps you should subclass UICollectionViewCell and create the subview in its viewDidLoad function? Maybe like this:

UIImageView *recipeImageView = [UIImageView alloc] init];
recipeImageView.frame = self.contentView.bounds;
[self.contentView addSubview:recipeImageView];
recipeImageView.tag = 100;

Then in your cellForItemAtIndexPath: you do this:

UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
recipeImageView.image = [UIImage imageNamed:[recipePhotos objectAtIndex:indexPath.row]];    

You don't have to add it back into the hierarchy; it's already there.

Edit:

Oh, Adam is also right about adding the imageView to the contentView rather than simply as a subview; I edited my code to reflect this.

Upvotes: 3

user189804
user189804

Reputation:

You should not be doing

[gallery1 registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:[NSString stringWithFormat:@"c%d",indexPath.row]];

in cellForItemAtIndexPath - you should register the class once and then dequeue cells according to the registration.

Do you have the images in your project? If you don't, imageNamed: won't find them. Check to see if the image has been loaded or is nil.

Finally, try adding the recipeImageView to the cell's contentView rather than as a subview (of the main view).

Upvotes: 0

Related Questions