【问题标题】:Trying to debug error: *** Assertion failure in -[UITableView _createPreparedCellForGlobalRow:withIndexPath:]尝试调试错误:*** 断言失败 -[UITableView _createPreparedCellForGlobalRow:withIndexPath:]
【发布时间】:2011-11-29 06:42:04
【问题描述】:

我有一个简单的 UITableviewController,它曾经可以正常工作,但现在有些东西损坏了它。 它提供了一个简单的表单,允许我将简单的 3 字段记录添加到核心数据托管对象。

当我添加一条记录时,它应该返回表格并显示内容。我可以看到它正在将记录写入数据库,但表为空,并出现以下错误。我可以通过单击“添加”按钮继续添加记录,但每次保存新记录并返回表时,它都是空的。

我看到的完整错误是这样的..

*** Assertion failure in -[UITableView _createPreparedCellForGlobalRow:withIndexPath:], /SourceCache/UIKit_Sim/UIKit-1448.89/UITableView.m:5678
2011-10-01 22:48:11.860 Gradetrack[59617:207] Serious application error.  An exception was caught from the delegate of NSFetchedResultsController during a call to -controllerDidChangeContent:.  UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath: with userInfo (null)

如果我停止模拟器并重新启动它,我可以从我的 NSLog 语句中看到它已从数据库中发现正确数量的记录并尝试加载表,但此时,我收到了类似的错误,并且下面的堆栈跟踪。

2011-10-01 23:08:50.332 Gradetrack[59795:207] >>> Enumber of courses entered thus far: 4 
2011-10-01 23:08:50.334 Gradetrack[59795:207] *** Assertion failure in -[UITableView _createPreparedCellForGlobalRow:withIndexPath:], /SourceCache/UIKit_Sim/UIKit-1448.89/UITableView.m:5678
2011-10-01 23:08:50.337 Gradetrack[59795:207] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'
*** Call stack at first throw:
(
    0   CoreFoundation                      0x00fd05a9 __exceptionPreprocess + 185
    1   libobjc.A.dylib                     0x01124313 objc_exception_throw + 44
    2   CoreFoundation                      0x00f88ef8 +[NSException raise:format:arguments:] + 136
    3   Foundation                          0x000e43bb -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 116
    4   UIKit                               0x0035ec91 -[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:withIndexPath:] + 883
    5   UIKit                               0x003544cc -[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:] + 75
    6   UIKit                               0x003698cc -[UITableView(_UITableViewPrivate) _updateVisibleCellsNow:] + 1561
    7   UIKit                               0x0036190c -[UITableView layoutSubviews] + 242
    8   QuartzCore                          0x01f70a5a -[CALayer layoutSublayers] + 181
    9   QuartzCore                          0x01f72ddc CALayerLayoutIfNeeded + 220
    10  QuartzCore                          0x01f180b4 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 310
    11  QuartzCore                          0x01f19294 _ZN2CA11Transaction6commitEv + 292
    12  UIKit                               0x002eb9c9 -[UIApplication _reportAppLaunchFinished] + 39
    13  UIKit                               0x002ebe83 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 690
    14  UIKit                               0x002f6617 -[UIApplication handleEvent:withNewEvent:] + 1533
    15  UIKit                               0x002eeabf -[UIApplication sendEvent:] + 71
    16  UIKit                               0x002f3f2e _UIApplicationHandleEvent + 7576
    17  GraphicsServices                    0x01928992 PurpleEventCallback + 1550
    18  CoreFoundation                      0x00fb1944 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
    19  CoreFoundation                      0x00f11cf7 __CFRunLoopDoSource1 + 215
    20  CoreFoundation                      0x00f0ef83 __CFRunLoopRun + 979
    21  CoreFoundation                      0x00f0e840 CFRunLoopRunSpecific + 208
    22  CoreFoundation                      0x00f0e761 CFRunLoopRunInMode + 97
    23  UIKit                               0x002eb7d2 -[UIApplication _run] + 623
    24  UIKit                               0x002f7c93 UIApplicationMain + 1160
    25  Gradetrack                          0x00002034 main + 102
    26  Gradetrack                          0x00001fc5 start + 53

根据 NSLog 语句,这是最后执行的子例程。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    NSLog(@">>> Enteringg %s [Line %d] ", __PRETTY_FUNCTION__, __LINE__);   

    id <NSFetchedResultsSectionInfo> sectionInfo = 
    [[_fetchedResultsController sections] objectAtIndex:section];
    NSLog(@">>> Enumber of courses entered thus far: %d ", [sectionInfo numberOfObjects]);  

    return [sectionInfo numberOfObjects];
}

注意:到目前为止输入的课程数量...的输出是 4,这是正确的。

这是我将单元格返回到 UITableViewController 的地方

- (UITableViewCell *)tableView:(UITableView *)tableView atIndexPath:(NSIndexPath *)indexPath {
    NSLog(@">>> Entering %s [Line %d] ", __PRETTY_FUNCTION__, __LINE__);    

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = 
    [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] 
                 initWithStyle:UITableViewCellStyleSubtitle 
                 reuseIdentifier:CellIdentifier] autorelease];
    }

    // Set up the cell...
    [tableView setAllowsSelectionDuringEditing:NO];
    [self configureCell:cell atIndexPath:indexPath];
    // load the cell with an appropriate image
    NSString *path = [[NSBundle mainBundle] pathForResource:@"studentprofile" ofType:@"png"];
    UIImage *theImage = [UIImage imageWithContentsOfFile:path];
    cell.imageView.image = theImage;

    return cell;
}

根据这些错误和我的堆栈跟踪,谁能指出我正确的方向?

非常感谢,

菲尔

【问题讨论】:

    标签: iphone core-data uitableview


    【解决方案1】:

    基于UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:,您可能忘记在实例化时设置单元标识符:

    cell = ((DetailCell *)[tableView dequeueReusableCellWithIdentifier:DetailCellIdentifier]);
    if (! cell)
    {
        // build the cell here
    }
    

    如果不是,代码的哪里出错了?

    【讨论】:

    • 谢谢。检查一下 - 我编辑了原始帖子以包含那段代码。
    【解决方案2】:

    好的,抱歉,SNAFU 警报。

    我不确定这是怎么发生的,但是如果您在我的“cellForRowAtIndexPath”函数声明中注意到...它只有 atIndexPath 而不是 cellForRowAtIndexPath。我一定是做了一些不明智的全局搜索和替换,结果毁了我的项目。

    现在工作。谢谢。

    问题 - 为什么代码编译时不会出现错误或警告 - 哇 - 这花了太长时间才能找到。

    【讨论】:

    • 您好 Phil,代码编译时不会出现错误或警告,因为从技术上讲,您可以使用“atIndexPath”而不是“cellForRowAtIndexPath”创建方法。显然,您的“atIndexPath”方法永远不会被 UITableViewDelegate 调用,但是创建自己的自定义方法并以此为名称是完全合法的。
    【解决方案3】:

    我刚遇到同样的问题,但我不小心声明:

    <UITableViewDelegate, UITableViewDelegate>
    

    而不是正确:

    <UITableViewDelegate, UITableViewDataSource>
    

    *update - 我还注意到在 xCode 4.3 中创建新 UITableViewController 类时生成的模板缺少 cellForRowAtIndexPath 中的关键项。那就是:

        if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    

    【讨论】:

    • 我不相信你现在需要手动分配一个表格视图单元格,因为出队调用总是会给你一个返回(如果需要它会创建一个)。
    • 如果这是真的,那肯定是另一个问题。如果不添加这个,我会崩溃。当我添加这个程序运行良好。如果我使用 UITableviewcontroller,也许这不需要,但否则呢?我必须进一步调查。
    • @theLastNightTrain 的信念不正确。从 dequeueReusableCellWithIdentifier 的文档中:“返回值一个带有关联标识符的 UITableViewCell 对象,如果可重用单元队列中不存在这样的对象,则返回 nil。”
    • 我的信念是基于使用 Storyboards 和 iOS 5 以及在 TableView 中定义的单元格模板 - 你要求一个带有标识符的单元格并且总是得到一个(假设标识符名称匹配:))。
    • 但是,如果您不使用情节提要,则确实需要在 dequeue... 返回 nil 时创建单元格。这也解决了我遇到的问题。很好的收获,@JoeFratianni。
    【解决方案4】:

    此错误的一个非常常见的原因是在 nib 文件中,顶级 UI 视图必须是 UITableViewCell 类型,如果不是这种情况,则会引发此断言错误。

    【讨论】:

      【解决方案5】:

      当我不小心删除了tableView:cellForRowAtIndexPath: 中的return cell 时,我得到了这个异常。当没有创建或返回单元格时会引发此异常。

      【讨论】:

        【解决方案6】:

        在我的例子中,iOS 5.0 ~ 6.0 和 storyboard 应该在 method

        之间有相同的单元格标识符
        (- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath) and **storyboard file**.
        
        - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
            static NSString *CellIdentifier = @"Cell"; ... }
        

        如果您使用@"Cell" 作为单元格标识符,则应使用情节提要文件属性检查器中相应 tableViewCell 的标识符处的名称。

        当你创建 UITableView 文件的子类时,默认代码使用@"Cell",而不是情节提要,空白。所以需要注意。

        【讨论】:

          【解决方案7】:

          我只是与这个断言搏斗了几个小时。我试图在同样位于笔尖的单元格中添加一个 UIButton 作为子视图。显然“自动布局”不喜欢这样,所以在我的笔尖中取消选中该选项后,我不再收到崩溃。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多