UICollectionView不显示单元格图像

Mur*_*lim 3 iphone ios uicollectionview

我有一个viewcontrolleruicollectionview它在里面.

我使用了以下代码,但没有可见的图像uicollectionview,只有黄色框:

myviewcontroller.h

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

@property (nonatomic, retain) IBOutlet UICollectionView *gallery1;
//@property (nonatomic, retain) IBOutlet UIImage *inimage;
Run Code Online (Sandbox Code Playgroud)

和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;
}
Run Code Online (Sandbox Code Playgroud)

我在uicollectionview中的单元格中看不到任何图像(图像文件存在).

谢谢你的帮助.

小智 5

这是我的看法:

- (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;
}
Run Code Online (Sandbox Code Playgroud)

imageView应该有一个框架...最好的选择是创建自己的框架.还要确保图像符合帧率并剪切到边界!