2013-06-04 55 views
0

我第一次創建了UICollectionView。我錯過了什麼?我試過了所有的東西,圖片肯定在應用程序中,正確命名並在數組中找到。然而他們沒有得到顯示:CollectionView不顯示圖標

我IconCell的標題:

#import <UIKit/UIKit.h> 

@interface IconSelectionCell : UICollectionViewCell 
@property (weak, nonatomic) IBOutlet UIImageView *iconImageView; 

@end 

實現我的IconCell:

#import "IconSelectionCell.h" 
#import <QuartzCore/QuartzCore.h> 

@implementation IconSelectionCell 

- (id)initWithFrame:(CGRect)frame { 
    self = [super initWithFrame:frame]; 
    if (self) { 
     self.restorationIdentifier = @"IconCell"; 
     self.backgroundColor = [UIColor clearColor]; 
     self.autoresizingMask = UIViewAutoresizingNone; 

     CGFloat borderWidth = 1.0f; 
     UIView *bgView = [[UIView alloc] initWithFrame:frame]; 
     bgView.layer.borderColor = [UIColor blueColor].CGColor; 
     bgView.layer.borderWidth = borderWidth; 
     self.selectedBackgroundView = bgView; 

     CGRect myContentRect = CGRectInset(self.contentView.bounds, borderWidth, borderWidth); 

     UIView *myContentView = [[UIView alloc] initWithFrame:myContentRect]; 
     myContentView.backgroundColor = [UIColor whiteColor]; 
     myContentView.layer.borderColor = [UIColor colorWithWhite:0.5f alpha:1.0f].CGColor; 
     myContentView.layer.borderWidth = borderWidth; 
     [self.contentView addSubview:myContentView]; 
    } 
    return self; 
} 

@end 

而且我collectionViewController:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *identifier = @"IconCell"; 

    IconSelectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath]; 

    cell.iconImageView.image = [UIImage imageNamed:[self.icons objectAtIndex:indexPath.row]]; 

    return cell; 
} 

我需要通過代碼添加imageview到單元格?我剛剛在界面生成器中創建了一個插座......或者我錯過了什麼?謝謝你的幫助!

+0

您是否在控制器的.m文件中註冊了您的單元類?你是否檢查過單元的initWithFrame方法是否被調用? – rdelmar

回答

1

我建議先試着讓它以編程方式工作。給你的IconSelectionCell類別一個UIImageView的財產(而不是IBOutlet)。然後在IconSelectionCellviewDidLoad方法,設置在UIImageView屬性以下屬性:(!)imageframecontentMode

如果你真的想使用Interface Builder和故事,確保插座正確掛接,並且原型單元格的身份檢查器在其類字段中具有您的自定義類名稱,並記得設置圖像。它更容易編程,因爲您可以佈置斷點並查看是否實際調用了代碼以及何時調用代碼。

+0

你能舉一個這個初始化如何編程的例子嗎?感謝 – MichiZH

+0

在你的界面: '@財產(強,非原子)的UIImageView * imageView' 在你實現的viewDidLoad: 'self.imageView.image = [UIImage的imageNamed:imageName]。 self.imageView.frame = self.bounds; self.contentMode = UIViewContentModeScaleAspectFit' 調整屬性以更改圖像的縱橫比和位置 –