UICollectionView añadir UICollectionCell


Cuando intento poner UICollectionCell a UICollectionView en Interface Builder no puedo ponerlo por razones desconocidas. La celda va a la barra de herramientas sin agregar a UICollectionView

Estoy usando:

  • iOS SDK 6.0
  • XCode 4.5.1
  • No uso Storyboard
Author: Matrosov Alexander, 2013-03-03

3 answers

Solo UICollectionView dentro del StoryBoard tiene UICollectionViewCell dentro. Si usa XIB, cree un nuevo XIB con CellName.xib, agregar CollectionViewCell, especificar el nombre de la clase personalizada UICollectionView. Después de que el uso registerNib.

Código de ejemplo: https://github.com/lequysang/TestCollectionViewWithXIB

 135
Author: LE SANG,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-03-03 11:09:40

No puede poner UICollectionViewCell directamente en el UiCollectionView si está utilizando el archivo Xib. Solo es posible en storyboard. Agregue un UICollectionViewCell en un archivo Xib separado. Da el nombre de tu clase. Luego registre class o xib antes de que aparezca la vista de colección

 [self.collectionView registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:CELL_ID];

Típicamente esto se hace en viewDidLoad.

Esta es la implementación de un custom UICollectionViewCell sin usar 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;
 }
 18
Author: Anil Varghese,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-02-13 04:13:34

Bien. En realidad, hay una solución para esto, si realmente quería tener la celda en collectionView dentro del archivo xib de interface builder:

  1. Crea un guion gráfico.
  2. Cree el UICollectionView y el UICollectionViewCell desde interface builder.
  3. Ajuste la interfaz de usuario con restricciones, etc. para que se vea exactamente como quería que fuera.
  4. Cree un nuevo archivo xib.
  5. Copie todo lo que hay dentro del storyboard en el nuevo archivo xib.

It funcionará perfectamente.

  • Una cosa a tener en cuenta que el paso # 3 es muy importante, porque después de #5 no se supone que arrastre y se mueva alrededor de la UICollectionView, si lo hace, la celda desaparecerá mágicamente! Aparte de eso, funcionará perfectamente.
 0
Author: RainCast,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-09-21 21:26:02