xcode 使用 dequeueReusableCellWithReuseIdentifier 的标识符问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19340823/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 04:05:12  来源:igfitidea点击:

issue with identifiers using dequeueReusableCellWithReuseIdentifier

iphoneiosobjective-cxcode

提问by Connor

I'm getting this runtime error when I run my app in the simulator.

当我在模拟器中运行我的应用程序时,我收到了这个运行时错误。

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'

The line it crashes on is

它崩溃的线路是

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

in the method

在方法中

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

As I understand it, the error means the indentifier "PlayingCard" doesn' match up with any of the identifiers of CollectionViewCells in in the CollectionView, but I have made sure that the identifier in the storyboard is identical.

据我了解,该错误意味着标识符“PlayingCard”与 CollectionView 中的任何 CollectionViewCells 标识符都不匹配,但我已确保故事板中的标识符相同。

Thanks for your help

谢谢你的帮助

回答by LE SANG

Your Error tell detail about your problem

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

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

If you create UICollectionViewCell class only by code, use register class in viewDidLoad for collectionView

如果仅通过代码创建 UICollectionViewCell 类,则在 viewDidLoad 中为 collectionView 使用 register 类

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

If you create UICollectionViewCell by Xib, use registerNib

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

If you drag UICollectionViewCell into storyboard, specify UICollectionView class and reuse identifier

如果将 UICollectionViewCell 拖入故事板,请指定 UICollectionView 类并重用标识符

I think you forgot to specify class name for your Cell in storyboard

我想你忘了在故事板中为你的 Cell 指定类名

回答by Adrian P

Here is a sample code of how to implement the cell with identifier for uicollectionview. I hope this helps out

以下是如何为 uicollectionview 实现带有标识符的单元格的示例代码。我希望这会有所帮助

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

Also there is a great tutorial on uicollectionview view in ray' web site that would help you understand the whole concept a bit more. Here is the link Tutorial

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

Let me know if this helps you out or you need more assistance my friend.

如果这对您有帮助,或者您需要更多帮助,我的朋友,请告诉我。

Edit:

编辑:

The issue with your project crashing was indeed in the storyboard part. You did everything correctly but your cell was never connected. It was an easy fix. I sent you back the project and left you a bit comments in there too. Let me know if you need any more assistance.

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

回答by Jesse Nelson

you should place [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell" ];in your init or view did load as stated above and be sure that cell in story board has the same name.

您应该[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell" ];按照上面的说明放置 在 init 或 view 中确实加载,并确保故事板中的单元格具有相同的名称。

your xml generated in your storyboard should look something like this (except collectionViewCell instead of tableViewCell)

你在故事板中生成的 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>

look at the tableViewCell section

查看 tableViewCell 部分