¿Cuál es el rol del método registerClass:forCellWithReuseIdentifier:
? De acuerdo con la documentación de desarrolladores de Apple que se supone queUICollectionView método registerClass: forCellWithReuseIdentifier rompe UICollectionView
"Registrar una clase para su uso en la creación de nuevas células de la vista de colección."
Cuando intento usarlo, obtengo una vista de colección negra. Cuando lo borro todo funciona bien.
#define cellId @"cellId"
#import "ViewController.h"
#import "Cell.h"
@interface ViewController()
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property(strong, nonatomic)NSMutableArray * photoArray;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSLog(@"%@",_photoArray);
_photoArray = [[NSMutableArray alloc]initWithCapacity:0];
[_collectionView registerClass:[Cell class] forCellWithReuseIdentifier:cellId];
for(int i=1;i<=12;i++)
{
NSString * imgName = [NSString stringWithFormat:@"%d.png",i];
UIImage *img = [UIImage imageNamed:imgName];
[_photoArray addObject:img];
}
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return _photoArray.count;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return 1;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
Cell* cell = [_collectionView dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath];
cell.cellImage.image = [_photoArray objectAtIndex:indexPath.row];
return cell;
}
¿Se está creando su vista de colección en IB? –
Sí, usando el guión gráfico –