项目中最近经常使用collectionView 但是系统的总是不满足要求,这时就需要我们自定义collectionView,根据不同的架构有不同的创建方式,我使用的是xib创建,那么问题来了:
- (void)awakeFromNib {
[_HLChoosePhotoActionSheetCollectionView registerClass:[HLChoosePhotoActionSheetCell class] forCellWithReuseIdentifier:@"HLChoosePhotoActionSheetCellID"];
}
在实例化方法中注册xib中的cell运行后,直接崩掉!抛出一下问题 找不到identifier标识的cell ,当时就蒙了,怎么会找不到呢?
后来研究才发现,原因在于注册cell的方法顺序错误,应该在
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
static NSString *HLChoosePhotoActionSheetCellIdentifier = @"HLChoosePhotoActionSheetCellID";
//在这里注册自定义的XIBcell 否则会提示找不到标示符指定的cell
UINib *nib = [UINib nibWithNibName:@"HLChoosePhotoActionSheetCell" bundle: [NSBundle mainBundle]];
[collectionView registerNib:nib forCellWithReuseIdentifier:HLChoosePhotoActionSheetCellIdentifier];
HLChoosePhotoActionSheetCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:HLChoosePhotoActionSheetCellIdentifier forIndexPath:indexPath];
[cell showDataWithImageName:@"weixin_app_icon"];
return cell;
}
这个代理方法里面进行注册自定义的cell。在运行,OK!跑起来了。