[英]UICollectionView doesn't show cell images
我有一個viewcontroller
和uicollectionview
。
我使用了以下代碼,但在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中的單元格中看不到任何圖像(圖像文件存在)。
謝謝你的幫助。
這是我的看法:
- (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應該有一個框架...最好的選擇是創建自己的框架。 還要確保圖像符合幀率並剪切到邊界!
@Adam是對的,每次制作新單元格時都不要調用registerClass
。 然而...
這里不對勁:
UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
recipeImageView.image = [UIImage imageNamed:[recipePhotos objectAtIndex:indexPath.row]];
[cell addSubview:recipeImageView];
您正在向單元格詢問子視圖 ,然后為該子視圖設置圖像,然后嘗試將該子視圖添加回層次結構......它已經存在?
該子視圖是否存在? 什么時候創建? 也許您應該將UICollectionViewCell
子類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而不是簡單地作為子視圖; 我編輯了我的代碼以反映這一點。
你不應該這樣做
[gallery1 registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:[NSString stringWithFormat:@"c%d",indexPath.row]];
在cellForItemAtIndexPath
- 您應該注冊一次類,然后根據注冊使單元格出列。
你的項目中有圖像嗎? 如果你不這樣做, imageNamed
:將找不到它們。 檢查圖像是否已加載或是否nil
。
最后,嘗試將recipeImageView
添加到單元格的contentView
而不是(主視圖的)子視圖。
小心:
這樣做的:
- (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個子視圖。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.