UICollectionView不显示单元格图像

时间:2013-07-07 21:47:25

标签: 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;

和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;
}

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

感谢您的帮助。

4 个答案:

答案 0 :(得分: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;
}

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

答案 1 :(得分:4)

@Adam是对的,每次制作新单元格时都不要调用registerClass。然而...

这里出了点问题:

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

您是asking the cell for a subview,然后为该子视图设置图像,然后尝试将该子视图添加回层次结构中......它已经存在?

该子视图是否存在?什么时候创建?也许您应该将UICollectionViewCell子类化并在其viewDidLoad函数中创建子视图?也许是这样的:

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

然后在cellForItemAtIndexPath:中执行此操作:

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

您不必将其添加回层次结构中;它已经存在了。

编辑:

哦,Adam也正确地将imageView添加到contentView而不仅仅是作为子视图;我编辑了我的代码以反映这一点。

答案 2 :(得分:0)

你不应该这样做

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

- 您应该注册一次类,然后根据注册将单元格出列。

你的项目中有图像吗?如果不这样做,imageNamed:将找不到它们。检查图像是否已加载或是nil

最后,尝试将recipeImageView添加到单元格的contentView而不是(主视图的)子视图。

答案 3 :(得分:0)

小心:

做的:

- (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;
}

你正在分配一个图像(它可以没问题......即使有点贵...)但每次都要添加,所以使用100次单元格会增加100个子视图。