使用取消排队的标识符问题重用单元格与重用标识符



我在模拟器中运行应用程序时收到此运行时错误。

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的任何标识符都不匹配,但我已确保故事板中的标识符相同。

感谢您的帮助

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

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

如果仅通过代码创建 UICollectionViewCell 类,请在 viewDidLoad 中使用 retype 类进行收集视图

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

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

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

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

下面是如何使用 uicollectionview 的标识符实现单元格的示例代码。我希望这有所帮助

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

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

编辑:

项目崩溃的问题确实在故事板部分。您正确地执行了所有操作,但您的手机从未连接过。这是一个容易解决的问题。我把项目发回给你,也给你留下了一些评论。

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

在情节提要中生成的 xml 应如下所示(集合视图单元格而不是表视图单元格除外)

<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>

查看表视图单元格部分

最新更新