Unable to dequeue a cell with identifier

4

here's the method causing the problem:

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"TableCell";
    TableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier 
                                                      forIndexPath:indexPath];

    // Configure the cell...
    int row = [indexPath row];

    Books *book = [myBooks objectAtIndex:row];
    cell.bookName.text = book.bookName;

    return cell;
}

full log:

    2014-02-05 23:19:09.458 Books[1425:a0b] *** Assertion failure in -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:], /SourceCache/UIKit_Sim/UIKit-2903.2/UITableView.m:5251
2014-02-05 23:19:09.471 Books[1425:a0b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier TableCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
*** First throw call stack:
(
    0   CoreFoundation                      0x017395e4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x014bc8b6 objc_exception_throw + 44
    2   CoreFoundation                      0x01739448 +[NSException raise:format:arguments:] + 136
    3   Foundation                          0x0109d23e -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 116
    4   UIKit                               0x003135e3 -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:] + 170
    5   Books                               0x000032df -[TableViewController tableView:cellForRowAtIndexPath:] + 127
    6   UIKit                               0x0031dd2f -[UITableView _createPreparedCellForGlobalRow:withIndexPath:] + 412
    7   UIKit                               0x0031de03 -[UITableView _createPreparedCellForGlobalRow:] + 69
    8   UIKit                               0x00302124 -[UITableView _updateVisibleCellsNow:] + 2378
    9   UIKit                               0x003155a5 -[UITableView layoutSubviews] + 213
    10  UIKit                               0x00299dd7 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 355
    11  libobjc.A.dylib                     0x014ce81f -[NSObject performSelector:withObject:] + 70
    12  QuartzCore                          0x03af372a -[CALayer layoutSublayers] + 148
    13  QuartzCore                          0x03ae7514 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
    14  QuartzCore                          0x03af3675 -[CALayer layoutIfNeeded] + 160
    15  UIKit                               0x00354ca3 -[UIViewController window:setupWithInterfaceOrientation:] + 304
    16  UIKit                               0x00273d27 -[UIWindow _setRotatableClient:toOrientation:updateStatusBar:duration:force:isRotating:] + 5212
    17  UIKit                               0x002728c6 -[UIWindow _setRotatableClient:toOrientation:updateStatusBar:duration:force:] + 82
    18  UIKit                               0x00272798 -[UIWindow _setRotatableViewOrientation:updateStatusBar:duration:force:] + 117
    19  UIKit                               0x00272820 -[UIWindow _setRotatableViewOrientation:duration:force:] + 67
    20  UIKit                               0x002718ba __57-[UIWindow _updateToInterfaceOrientation:duration:force:]_block_invoke + 120
    21  UIKit                               0x0027181c -[UIWindow _updateToInterfaceOrientation:duration:force:] + 400
    22  UIKit                               0x00272573 -[UIWindow setAutorotates:forceUpdateInterfaceOrientation:] + 870
    23  UIKit                               0x00275b66 -[UIWindow setDelegate:] + 449
    24  UIKit                               0x00346dc7 -[UIViewController _tryBecomeRootViewControllerInWindow:] + 180
    25  UIKit                               0x0026b7cc -[UIWindow addRootViewControllerViewIfPossible] + 609
    26  UIKit                               0x0026b947 -[UIWindow _setHidden:forced:] + 312
    27  UIKit                               0x0026bbdd -[UIWindow _orderFrontWithoutMakingKey] + 49
    28  UIKit                               0x0027644a -[UIWindow makeKeyAndVisible] + 65
    29  UIKit                               0x002298e0 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1851
    30  UIKit                               0x0022dfb8 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 824
    31  UIKit                               0x0024242c -[UIApplication handleEvent:withNewEvent:] + 3447
    32  UIKit                               0x00242999 -[UIApplication sendEvent:] + 85
    33  UIKit                               0x0022fc35 _UIApplicationHandleEvent + 736
    34  GraphicsServices                    0x0368c2eb _PurpleEventCallback + 776
    35  GraphicsServices                    0x0368bdf6 PurpleEventCallback + 46
    36  CoreFoundation                      0x016b4dd5 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 53
    37  CoreFoundation                      0x016b4b0b __CFRunLoopDoSource1 + 523
    38  CoreFoundation                      0x016df7ec __CFRunLoopRun + 2156
    39  CoreFoundation                      0x016deb33 CFRunLoopRunSpecific + 467
    40  CoreFoundation                      0x016de94b CFRunLoopRunInMode + 123
    41  UIKit                               0x0022d6ed -[UIApplication _run] + 840
    42  UIKit                               0x0022f94b UIApplicationMain + 1225
    43  Books                               0x000038bd main + 141
    44  libdyld.dylib                       0x01d75725 start + 0
    45  ???                                 0x00000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 
ios
objective-c
uitableview
asked on Stack Overflow Feb 5, 2014 by thirdage • edited Feb 6, 2014 by Matthias Bauch

3 Answers

22

You can directly set the identifier using the storyboard like this.

enter image description here

answered on Stack Overflow Feb 5, 2014 by bsarr007
12

From the documentation:

Important: You must register a class or nib file using the registerNib:forCellReuseIdentifier: or registerClass:forCellReuseIdentifier: method before calling this method.

You need to register the cell as a usable class for that identifier. Call this after you initialize your UITableView (probably in viewDidLoad)

[self.tableView registerClass:[TableCell class] forCellReuseIdentifier:CellIdentifier]

This assumes CellIdentifier has been moved somewhere where it can be accessed from here.

answered on Stack Overflow Feb 5, 2014 by Dima
3

Try this:

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

static NSString *CellIdentifier = @"TableCell";
TableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if(!cell)
{
    cell = [[[TableCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}

// Configure the cell...
int row = [indexPath row];

Books *book = [myBooks objectAtIndex:row];
cell.bookName.text = book.bookName;

return cell;
}
answered on Stack Overflow Feb 5, 2014 by Gago

User contributions licensed under CC BY-SA 3.0