【xib】加载的一些问题,以UICollectionView 为例

xib 用不好在开发中很容易制造各种crash,issue像

1.[CUINamedImage collectionView:numberOfItemsInSection:]: unrecognized selector sent to instance 0x8c9a6d0
2.*** Assertion failure in -[UICollectionView _dequeueReusableViewOfKind:withIdentifier:forIndexPath:]
3.[UICollectionView _dequeueReusableViewOfKind:withIdentifier:forIndexPath:viewCategory:]

 

我想在一个ViewController中自定义一个View,这个View是collectionview

//.h文件
@interface ImageCollectionView : UICollectionView<UICollectionViewDataSource,UICollectionViewDelegate>
@property (weak, nonatomic) IBOutlet UICollectionView *imageCollection;
+(ImageCollectionView *)instanceTextView;
@end

//.m文件
#import "ImageCollectionView.h"
#import "ImageCollectionViewCell.h"

@implementation ImageCollectionView
+(ImageCollectionView *)instanceTextView
{
    NSArray* nibView =  [[NSBundle mainBundle] loadNibNamed:@"ImageCollectionView" owner:nil options:nil];
    return [nibView objectAtIndex:0];
}
-(id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if(self)
    {
    }
    return self;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 12;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    static NSString * CellIdentifier = @"CustomCollectCell";
    
    [_imageCollection registerNib:[UINib nibWithNibName:@"ImageCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:CellIdentifier];
    ImageCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
    
    return cell;
    
}

//ImageCollectionViewCell 就不写了

  

在看xib

在image Collection View 中inspector 继承ImageCollectionView,

在collection View 中outlets 连image Collection View而不是File‘s owner (一般自带xib的控制器都是连File‘s owner)

 

上面是用xib自定义一个collectionView 的例子。

再看看 创建一个自带xib的ControllerView的情况:

File‘s owner 中custom class 是绑定控制器类AddGoodsController的,连一些协议也就是连 File‘s owner 。

 

 

所以,

1.绑定谁,谁就是self,可以在类中self.  

2.用xib 自定义view 出现上面的问题基本就是绑定关系的问题

3.xib技巧介绍

 

 

转载于:https://www.cnblogs.com/-yfan/p/4663914.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值