Crear la subclase UICollectionViewCell con xib [duplicar]
Esta pregunta ya tiene una respuesta aquí:
¿Qué significa esto? “'NSUnknownKeyException', motivo: ... esta clase no es compatible con el valor de clave para la clave X” 66 respuestasEstoy tratando de crear unUICollectionViewCell
subclase con un xib vinculado, tengo que hacer esto: he creado un nuevo archivo xib y he añadido unUICollectionViewCell
en él, entonces he creado este archivo de subclases:
@interface MyCell : UICollectionViewCell
@property (weak, nonatomic) IBOutlet UILabel *label;
@end
También he vinculado en la clase personalizada del propietario del archivo elMyCell
clase en el constructor de interfaces, y he añadido unUILabel
, luego en miUICollectionView
viewDidLoad hago esto:
[self.collectionView registerClass:[MyCell class] forCellWithReuseIdentifier:@"MyCell"];
UINib *cellNib = [UINib nibWithNibName:@"MyCell" bundle:nil];
[self.collectionView registerNib:cellNib forCellWithReuseIdentifier:@"MyCell"];
Así como en esto:
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
MyCell *cell = (MyCell*)[cv dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath];
cell.label.text = @"Cell Text";
return cell;
}
Sin embargo esto no funciona, recibo este error:
*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<NSObject 0x907eca0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key label.'
¿Qué hice mal? ¿Cómo puedo conectar unUICollectionViewCell
subclase a un xib, y visualizarlo en unaUICollectionView
?
EDITAR:
Tengo que hacer esto:
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
NSString *identifier = @"MyCell";
static BOOL nibMyCellloaded = NO;
if(!nibMyCellloaded)
{
UINib *nib = [UINib nibWithNibName:@"MyCell" bundle: nil];
[cv registerNib:nib forCellWithReuseIdentifier:identifier];
nibMyCellloaded = YES;
}
MyCell *cell = (MyCell*)[cv dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath];
cell.labelCell.text = @"Text";
return cell;
}