从 XIB 加载时 UICollectionViewCell 导致崩溃

And*_*rew 1 xcode objective-c xib ios uicollectionview

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

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

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

有了这个:

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

    [self.localPlayerItemsView registerNib:nib forCellWithReuseIdentifier:CellIdentifier];
}
Run Code Online (Sandbox Code Playgroud)

这样做后,我会在第一行崩溃collectionView:cellForItemAtIndexPath:

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

    return aCell;
}
Run Code Online (Sandbox Code Playgroud)

这是崩溃:

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

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

gab*_*ler 5

在您的MBTradeCollectionViewCellxib 文件中,不要更改文件所有者的任何内容。将top most view的类更改为 MBTradeCollectionViewCell,然后选择itemCountView,将其连接到您的top most view. 希望这能解决问题。