Connor
Connor

Reputation: 64644

issue with identifiers using dequeueReusableCellWithReuseIdentifier

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.

Thanks for your help

Upvotes: 2

Views: 9190

Answers (3)

Adrian P
Adrian P

Reputation: 6529

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

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

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.

Upvotes: 4

Jesse Nelson
Jesse Nelson

Reputation: 796

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.

your xml generated in your storyboard should look something like this (except collectionViewCell instead of 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

Upvotes: 1

LE SANG
LE SANG

Reputation: 11005

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

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

If you create UICollectionViewCell by Xib, use registerNib

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

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

Upvotes: 7

Related Questions