2

当我在模拟器中运行我的应用程序时出现此运行时错误。

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'could not dequeue a view of kind: UICollectionElementKindCell with identifier PlayingCard - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'

它崩溃的行是

UICollectionViewCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PlayingCard"
                                                                       forIndexPath:indexPath];

在方法中

-(UICollectionViewCell*) collectionView:(UICollectionView *)collectionView
             cellForItemAtIndexPath:(NSIndexPath *)indexPath{}

据我了解,该错误意味着标识符“PlayingCard”与 CollectionView 中的任何 CollectionViewCells 标识符都不匹配,但我确保情节提要中的标识符是相同的。

谢谢你的帮助

4

3 回答 3

7

您的错误详细说明您的问题

must register a nib or a class for the identifier or connect a prototype cell in a storyboard

如果您仅通过代码创建 UICollectionViewCell 类,请在 viewDidLoad 中为 collectionView 使用注册类

 [self.collectionView registerClass:[YourCell class] forCellWithReuseIdentifier:@"PlayingCard"];

如果您通过 Xib 创建 UICollectionViewCell,请使用 registerNib

如果将 UICollectionViewCell 拖到情节提要中,请指定 UICollectionView 类并重用标识符

我认为您忘记在情节提要中为 Cell 指定类名

于 2013-10-13T01:46:34.353 回答
4

这是一个示例代码,说明如何使用 uicollectionview 的标识符实现单元格。我希望这会有所帮助

 - (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath {
 UICollectionViewCell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"YourIdentifier" forIndexPath:indexPath];
cell.backgroundColor = [UIColor whiteColor];
return cell; 
}

在 ray 的网站上还有一个很棒的关于 uicollectionview 视图的教程,可以帮助您更多地理解整个概念。这是链接教程

让我知道这是否对您有所帮助,或者您需要更多帮助,我的朋友。

编辑:

您的项目崩溃的问题确实在情节提要部分。你做的一切都是正确的,但你的手机从未连接过。这是一个简单的修复。我把项目发回给你,也给你留下了一些评论。如果您需要更多帮助,请告诉我。

于 2013-10-13T04:47:31.487 回答
1

你应该把 [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell" ]; 你的 init 或视图放在上面,并确保故事板中的单元格具有相同的名称。

您在情节提要中生成的 xml 应该看起来像这样(除了 collectionViewCell 而不是 tableViewCell)

<tableViewController id="qqN-Qz-7Na" customClass="ColorPickerSavedColorTableViewController" sceneMemberID="viewController">
                <tableView key="view" opaque="NO" clipsSubviews="YES" clearsContextBeforeDrawing="NO" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="prototypes" style="plain" separatorStyle="default" rowHeight="44" sectionHeaderHeight="22" sectionFooterHeight="22" id="jm9-vU-9fK" userLabel="TableView">
                    <rect key="frame" x="0.0" y="0.0" width="320" height="568"/>
                    <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
                    <color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
                    <prototypes>
                        <tableViewCell contentMode="scaleToFill" selectionStyle="default" indentationWidth="10" reuseIdentifier="Cell" id="8ty-Ap-v04">
                            <rect key="frame" x="0.0" y="22" width="320" height="44"/>
                            <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
                            <tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="8ty-Ap-v04" id="Omv-J2-1dd">
                                <rect key="frame" x="0.0" y="0.0" width="320" height="43"/>
                                <autoresizingMask key="autoresizingMask"/>
                            </tableViewCellContentView>
                        </tableViewCell>
                    </prototypes>
                </tableView>
                <navigationItem key="navigationItem" id="6MG-Do-Mu4"/>
            </tableViewController>

查看 tableViewCell 部分

于 2013-10-13T07:55:34.893 回答