Jaka jest rola metody registerClass:forCellWithReuseIdentifier:
? Zgodnie z dokumentacją programistyczną firmy Apple ma ona na celu:UICollectionView registerClass: Metoda forCellWithReuseIdentifier łamie UICollectionView
"Zarejestruj klasę do wykorzystania w tworzeniu nowych komórek widoku kolekcji."
Kiedy próbuję go użyć, otrzymuję widok czarnej kolekcji. Kiedy go usuwam, wszystko działa poprawnie.
#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;
}
Czy Twój widok kolekcji jest tworzony w IB? –
Tak, używając storyboardu –