Vous ne pouvez pas mettre UICollectionViewCell
directement dans le UiCollectionView
si vous utilisez le fichier Xib
. C'est possible seulement en storyboard. Ajoutez un UICollectionViewCell
dans un fichier Xib distinct. Donnez le nom de votre classe. Alors inscrivez-vous ou l'autre classe ou avant que la vue xib de collection apparaît
[self.collectionView registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:CELL_ID];
En général cela se fait dans viewDidLoad
.
C'est la mise en œuvre d'une coutume UICollectionViewCell
avec l'aide Xib
@implementation CollectionViewCell
@synthesize imageView = _imageView;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor whiteColor];
self.layer.shadowColor = [UIColor blackColor].CGColor;
self.layer.shadowRadius = 5.0f;
self.layer.shadowOffset = CGSizeMake(2.0f, 2.0f);
self.layer.shadowOpacity = 0.5f;
// Selected background view
UIView *backgroundView = [[UIView alloc]initWithFrame:self.bounds];
backgroundView.layer.borderColor = [[UIColor colorWithRed:0.529 green:0.808 blue:0.922 alpha:1]CGColor];
backgroundView.layer.borderWidth = 10.0f;
self.selectedBackgroundView = backgroundView;
// set content view
CGRect frame = CGRectMake(self.bounds.origin.x+5, self.bounds.origin.y+5, self.bounds.size.width-10, self.bounds.size.height-10);
UIImageView *imageView = [[UIImageView alloc] initWithFrame:frame];
self.imageView = imageView;
[imageView release];
self.imageView.contentMode = UIViewContentModeScaleAspectFill ;
self.imageView.clipsToBounds = YES;
[self.contentView addSubview:self.imageView];
}
return self;
}
[Voici une simple procédure pas à pas] (http://stackoverflow.com/a/31735229/3681880) pour créer un objet UICollectionViewCell dans un objet UICollectionView. – Suragch