What does this assertion failure mean in a UITableViewDataSource?

-3

Currently following a checklists tutorial built with io6 in mind. I'm using xcode 5, ios7 sdk. The tutorial hasn't been updated for IOS7 but I didn't want to stop my learning so decided to go ahead and work with the outdated tutorial and hopefully use it as a learning experience. Using reading of official Apple docs and extensive googling as my guide along the way.

Very early I've run into an issue already and not sure what is wrong. I noticed that the autocomplete had part of the methods below crossed out (a deprecation maybe?). The issue is definitely coming from the code below because once I remove it the simulator loads the app fine.

Code causing crash:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ChecklistItem"];
    return cell;
}

Here is the stack trace:

2013-09-28 20:51:26.208 Checklists[47289:a0b] *** Assertion failure in -[UITableView _configureCellForDisplay:forIndexPath:], /SourceCache/UIKit_Sim/UIKit-2903.2/UITableView.m:6235
2013-09-28 20:51:26.218 Checklists[47289:a0b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'
*** First throw call stack:
(
    0   CoreFoundation                      0x017335e4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x014b68b6 objc_exception_throw + 44
    2   CoreFoundation                      0x01733448 +[NSException raise:format:arguments:] + 136
    3   Foundation                          0x0109723e -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 116
    4   UIKit                               0x00311ae5 __53-[UITableView _configureCellForDisplay:forIndexPath:]_block_invoke + 426
    5   UIKit                               0x0028af5f +[UIView(Animation) performWithoutAnimation:] + 82
    6   UIKit                               0x0028afa8 +[UIView(Animation) _performWithoutAnimation:] + 40
    7   UIKit                               0x00311936 -[UITableView _configureCellForDisplay:forIndexPath:] + 108
    8   UIKit                               0x00317d4d -[UITableView _createPreparedCellForGlobalRow:withIndexPath:] + 442
    9   UIKit                               0x00317e03 -[UITableView _createPreparedCellForGlobalRow:] + 69
    10  UIKit                               0x002fc124 -[UITableView _updateVisibleCellsNow:] + 2378
    11  UIKit                               0x0030f5a5 -[UITableView layoutSubviews] + 213
    12  UIKit                               0x00293dd7 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 355
    13  libobjc.A.dylib                     0x014c881f -[NSObject performSelector:withObject:] + 70
    14  QuartzCore                          0x03aed72a -[CALayer layoutSublayers] + 148
    15  QuartzCore                          0x03ae1514 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
    16  QuartzCore                          0x03aed675 -[CALayer layoutIfNeeded] + 160
    17  UIKit                               0x0034eca3 -[UIViewController window:setupWithInterfaceOrientation:] + 304
    18  UIKit                               0x0026dd27 -[UIWindow _setRotatableClient:toOrientation:updateStatusBar:duration:force:isRotating:] + 5212
    19  UIKit                               0x0026c8c6 -[UIWindow _setRotatableClient:toOrientation:updateStatusBar:duration:force:] + 82
    20  UIKit                               0x0026c798 -[UIWindow _setRotatableViewOrientation:updateStatusBar:duration:force:] + 117
    21  UIKit                               0x0026c820 -[UIWindow _setRotatableViewOrientation:duration:force:] + 67
    22  UIKit                               0x0026b8ba __57-[UIWindow _updateToInterfaceOrientation:duration:force:]_block_invoke + 120
    23  UIKit                               0x0026b81c -[UIWindow _updateToInterfaceOrientation:duration:force:] + 400
    24  UIKit                               0x0026c573 -[UIWindow setAutorotates:forceUpdateInterfaceOrientation:] + 870
    25  UIKit                               0x0026fb66 -[UIWindow setDelegate:] + 449
    26  UIKit                               0x00340dc7 -[UIViewController _tryBecomeRootViewControllerInWindow:] + 180
    27  UIKit                               0x002657cc -[UIWindow addRootViewControllerViewIfPossible] + 609
    28  UIKit                               0x00265947 -[UIWindow _setHidden:forced:] + 312
    29  UIKit                               0x00265bdd -[UIWindow _orderFrontWithoutMakingKey] + 49
    30  UIKit                               0x0027044a -[UIWindow makeKeyAndVisible] + 65
    31  UIKit                               0x002238e0 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1851
    32  UIKit                               0x00227fb8 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 824
    33  UIKit                               0x0023c42c -[UIApplication handleEvent:withNewEvent:] + 3447
    34  UIKit                               0x0023c999 -[UIApplication sendEvent:] + 85
    35  UIKit                               0x00229c35 _UIApplicationHandleEvent + 736
    36  GraphicsServices                    0x036862eb _PurpleEventCallback + 776
    37  GraphicsServices                    0x03685df6 PurpleEventCallback + 46
    38  CoreFoundation                      0x016aedd5 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 53
    39  CoreFoundation                      0x016aeb0b __CFRunLoopDoSource1 + 523
    40  CoreFoundation                      0x016d97ec __CFRunLoopRun + 2156
    41  CoreFoundation                      0x016d8b33 CFRunLoopRunSpecific + 467
    42  CoreFoundation                      0x016d894b CFRunLoopRunInMode + 123
    43  UIKit                               0x002276ed -[UIApplication _run] + 840
    44  UIKit                               0x0022994b UIApplicationMain + 1225
    45  Checklists                          0x00001b7d main + 141
    46  libdyld.dylib                       0x01d6f725 start + 0
)
libc++abi.dylib: terminating with uncaught exception of type NSException
objective-c
cocoa-touch
uitableview
uikit
asked on Stack Overflow Sep 28, 2013 by LondonGuy • edited Sep 29, 2013 by Matthias Bauch

4 Answers

1

I guess you didn't set an identifier on you cell in the storyboard, and it crashes because it can't instantiate a cell with an identifier that doesn't exists

answered on Stack Overflow Sep 28, 2013 by jonas vermeulen
1

dequeueReusableCellWithIdentifier: is used to reuse table view cells. If there's not a cell to reuse, this method returns nil, and you have to manually create the table view cell and return it. When there is a table view cell which is identical to another one that was used previously, dequeueReusableCellWithIdentifier: may return a valid cell.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ChecklistItem"];
    if(!cell)
        cell= [[UITableViewCell alloc]initWithStyle: UITableViewCellStyleDefault reuseIdentifier: @"ChecklistItem"];
    return cell;
}
answered on Stack Overflow Sep 28, 2013 by Ramy Al Zuhouri
1

It means that

UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:

as the error message says.

dequeueReusableCellWithIdentifier: is not guaranteed to always return a cell since it may return nil.

If you are supporting iOS >= 6, change it with dequeueReusableCellWithIdentifier:forIndexPath: which will never return nil.

The documentation is pretty clear about it.

To wrap it up your code should be

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ChecklistItem" 
                                                            forIndexPath:indexPath];
    /* Configure the cell here */
    return cell;
}
answered on Stack Overflow Sep 28, 2013 by Gabriele Petronella • edited Sep 28, 2013 by Gabriele Petronella
0

You need to set the class as the datasource for the tableview. You can do this by control dragging from the tableview in the storyboard to the viewController icon on the bottom (far left) and click dataSource and delegate.

answered on Stack Overflow Sep 28, 2013 by AdamG

User contributions licensed under CC BY-SA 3.0