方法的作用是registerClass:forCellWithReuseIdentifier:
什么?根据 Apple 的开发人员文档,它应该
“注册一个用于创建新集合视图单元格的类。”
当我尝试在我的项目中使用它时,我得到了一个黑色的集合视图。当我删除它时,一切正常。
#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;
}