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

4 回答 4

1

我猜您没有在情节提要中的单元格上设置标识符,并且它崩溃了,因为它无法实例化具有不存在标识符的单元格

于 2013-09-28T21:03:56.147 回答
1

dequeueReusableCellWithIdentifier:用于重用表格视图单元格。如果没有要重用的单元格,则此方法返回nil,您必须手动创建表格视图单元格并将其返回。当存在与之前使用的另一个相同的表格视图单元格时,dequeueReusableCellWithIdentifier:可能会返回一个有效的单元格。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ChecklistItem"];
    if(!cell)
        cell= [[UITableViewCell alloc]initWithStyle: UITableViewCellStyleDefault reuseIdentifier: @"ChecklistItem"];
    return cell;
}
于 2013-09-28T21:04:50.387 回答
1

代表着

UITableViewdataSource 必须返回一个单元格tableView:cellForRowAtIndexPath:

如错误消息所述。

dequeueReusableCellWithIdentifier:不保证总是返回一个单元格,因为它可能会返回nil

如果您支持 iOS >= 6,请将其更改为dequeueReusableCellWithIdentifier:forIndexPath:which will never return nil

文档对此非常清楚。

总结你的代码应该是

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ChecklistItem" 
                                                            forIndexPath:indexPath];
    /* Configure the cell here */
    return cell;
}
于 2013-09-28T21:04:56.277 回答
0

您需要将类设置为 tableview 的数据源。您可以通过控制从情节提要中的 tableview 拖动到底部(最左侧)的 viewController 图标并单击 dataSource 和委托来执行此操作。

于 2013-09-28T21:02:04.123 回答