【问题标题】:What does this assertion failure mean in a UITableViewDataSource?这个断言失败在 UITableViewDataSource 中意味着什么?
【发布时间】:2013-09-28 20:58:23
【问题描述】:

目前正在关注使用 io6 构建的清单教程。我正在使用 xcode 5、ios7 sdk。本教程尚未针对 IOS7 进行更新,但我不想停止学习,因此决定继续使用过时的教程,并希望将其用作学习体验。在此过程中,我使用阅读官方 Apple 文档和广泛的谷歌搜索作为我的指南。

我很早就遇到了问题,但不确定是什么问题。我注意到自动完成功能将下面的部分方法划掉了(可能是弃用?)。问题肯定来自下面的代码,因为一旦我删除它,模拟器就会很好地加载应用程序。

导致崩溃的代码:

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

这是堆栈跟踪:

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


【解决方案1】:

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

【讨论】:

  • 这是个问题.. 很好。我拼错了标识符。将 checkistItem 编辑为 checklistItem 解决了这个问题。谢谢。
【解决方案2】:

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;
}

【讨论】:

  • 这是旧的做法。 Apple 通过从 iOS 6 开始提供dequeueReusableCellWithIdentifier:forIndexPath: 大大简化了这一点。如果您为该标识符注册了一个单元格,API 将自动为您实例化一个。
【解决方案3】:

意思是

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

如错误消息所述。

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

如果您支持 iOS >= 6,请将其更改为 dequeueReusableCellWithIdentifier:forIndexPath:,它永远不会返回 nil。

documentation 很清楚。

总结你的代码应该是

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ChecklistItem" 
                                                            forIndexPath:indexPath];
    /* Configure the cell here */
    return cell;
}

【讨论】:

    【解决方案4】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-12
      • 2012-05-29
      相关资源
      最近更新 更多