0

First time I've created a UICollectionView..what am I missing? I've tried everything, the images are definitely in the app, correctly named and found in the array. Nevertheless they don't get displayed:

Header of my IconCell:

#import <UIKit/UIKit.h>

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

@end

Implementation my 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

And my 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;
}

Do I need to add the imageview via code to the cell? I've just created an outlet in the Interface Builder…or what am I missing? Thanks for your help!

4

1 回答 1

1

我建议首先尝试让它以编程方式工作。给你的IconSelectionCell班级一个属性(不是 IBOutlet)UIImageView。然后在 的viewDidLoad方法中IconSelectionCell,在属性上设置以下属性UIImageViewimage, frame(!), andcontentMode

如果你真的想使用 Interface Builder 和故事板,请确保插座连接正确,并且原型单元的身份检查器在其类字段中具有您的自定义类名称,并记住设置图像。以编程方式更容易,因为您可以布置断点并查看代码是否实际被调用以及何时被调用。

于 2013-06-04T15:46:08.993 回答