UICollectionViewCell从XIB加载时导致崩溃

时间:2014-10-17 14:25:18

标签: ios objective-c xcode uicollectionview xib

我有一个UICollectionViewCell子类,它现在只显示一个带有背景颜色的UIView子视图,以显示它在那里。

为了从XIB加载,我必须替换它:

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.localPlayerItemsView registerClass:[MBTradeCollectionViewCell class]
                  forCellWithReuseIdentifier:CellIdentifier];
}

用这个:

- (void)viewDidLoad {
    [super viewDidLoad];
    UINib *nib = [UINib nibWithNibName:@"MBTradeCollectionViewCell" bundle:nil];

    [self.localPlayerItemsView registerNib:nib forCellWithReuseIdentifier:CellIdentifier];
}

这样做后,我在collectionView:cellForItemAtIndexPath:的第一行发生了崩溃:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    MBTradeCollectionViewCell *aCell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier
                                                                                forIndexPath:indexPath];

    return aCell;
}

这是崩溃:

*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<NSObject 0x7d461780> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key itemCountView.'

使用registerClass:forCellWithReuseIdentifier:时不会导致此崩溃,但它不会加载我的xib。

3 个答案:

答案 0 :(得分:4)

MBTradeCollectionViewCell xib文件中,请勿使用文件所有者更改任何内容。将top most view的类更改为MBTradeCollectionViewCell,然后选择itemCountView,将其连接到top most view。希望这能解决问题。

答案 1 :(得分:3)

当您的插座损坏时,通常会发生此故障。检查您的nib文件,找到名称为itemCountView的插座,该插座会损坏(未正确连接)。哪个会出现红色标记。

答案 2 :(得分:1)

我修好了。

我进入助理模式,从房产拖到IB,它为我提供了“文件所有者”或“交易集合视图”选项?

我选择了底部的一个,这意味着那些东西没有挂在文件的所有者身上,但事实证明它们在贸易集合视图中也是可见的,并且它们被连接在那里。

它有效。