3

我有一个viewcontrolleruicollectionview

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

我的视图控制器.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

4 回答 4

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 应该有一个框架......最好的选择是创建自己的框架。还要确保图像符合帧比并剪辑到边界!

于 2013-10-06T09:35:51.580 回答
4

@Adam 是对的,不要在registerClass每次创建新单元时都打电话。然而...

这里不对劲:

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

您正在向单元格询问子视图,然后为该子视图设置图像,然后尝试将该子视图添加回层次结构......它已经在哪里了?

那个子视图甚至存在吗?它是什么时候创建的?也许您应该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 而不是简单地作为子视图也是正确的;我编辑了我的代码以反映这一点。

于 2013-07-08T01:45:35.437 回答
0

你不应该这样做

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

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

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

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

于 2013-07-08T01:33:16.887 回答
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 个子视图。

于 2014-05-15T12:23:27.010 回答