2013-06-04 37 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' 调整属性以更改图像的纵横比和位置 –