Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 472
Murat Calim Avatar asked Jan 19 '26 10:01

Murat Calim


1 Answers

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!

like image 122
leokash Avatar answered Jan 21 '26 02:01

leokash



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!