2013-07-07 122 views
3

我有一個viewcontrolleruicollectionview在裏面。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; 
} 

我在c中看不到任何圖像(圖像文件已存在)在uicollectionview中的ells。

感謝您的任何幫助。

+0

對你來說這個決心,穆拉特? –

+0

是的,thanx馬特。 –

回答

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]];  

您不必添加回層次;它已經在那裏了。

編輯:

哦,亞當也說得對添加的ImageView的內容查看,而不是簡單地作爲一個子視圖;我編輯我的代碼來反映這一點。

+0

你說得對,UIImageView不會有效 - 必須按照你的展示創建。 –

+1

@AdamEberbach你說得對,自定義內容進入'contentView',按照http://developer.apple.com/library/ios/documentation/uikit/reference/UICollectionViewCell_class/Reference/Reference.html#//apple_ref/occ/instp/UICollectionViewCell/contentView謝謝! –

0

你不應該在cellForItemAtIndexPath

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

- 你應該根據註冊一次,然後註冊類離隊細胞。

你的項目中有圖像嗎?如果你沒有,imageNamed:不會找到它們。檢查圖像是否已經加載或者是nil

最後,嘗試將recipeImageView添加到單元的contentView而不是作爲子視圖(主視圖)。

+0

感謝您的回答亞當,是的,我已經在項目文件夾中的圖像,但我無法理解[gallery1 registerClass:[UICollectionViewCell類] forCellWithReuseIdentifier:[NSString stringWithFormat:@「c%d」,indexPath.row]];使用和註冊一次。我不確定是否必須將該行移到viewLoad中?還有一件事:self.contentView沒有找到,我之前沒有聲明/使用。 –

+0

viewDidLoad通常是使用registerClass的地方,是的。你應該使用cell.contentView(不是self.contentView)。 –

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的應該有一個框架...最好的辦法是創建自己的框架。 也確保圖像將適合框架比例和剪輯的邊界!

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個子視圖。