使用xib创建CollectionViewCell注意事项

项目中最近经常使用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!跑起来了。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值