registerClass:forCellWithReuseIdentifier:方法的作用是什么?根据苹果的开发者文档,这应该是
“注册一个类,用于创建新的集合视图单元格。”
当我试图使用它,我的项目,我得到一个黑色的集合视图。当我删除它时,一切都很好。
#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;
}发布于 2013-01-20 10:06:12
同意RazorSharp的回答的观点,并想指出,在Techtopia链接中,对我来说最关键的短语是:
如果单元格类是用代码编写的,则使用registerClass: UICollectionView方法执行注册,否则使用registerNib。
https://stackoverflow.com/questions/12865196
复制相似问题