Non è possibile inserire UICollectionViewCell
direttamente nello UiCollectionView
se si utilizza il file Xib
. È possibile solo nello storyboard. Aggiungi un UICollectionViewCell
in un file Xib separato. Dai il tuo nome alla classe. Quindi registrare o classe o xib prima che la vista collezione appare
[self.collectionView registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:CELL_ID];
In genere questo viene fatto in viewDidLoad
.
Questa è l'implementazione di un costume UICollectionViewCell
senza l'uso di 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;
}
fonte
2013-03-03 11:08:54
[Ecco una semplice soluzione] (http://stackoverflow.com/a/31735229/3681880) per creare un 'UICollectionViewCell' in un' UICollectionView'. – Suragch