【问题标题】:iOS 5 Storyboard Custom Cell Crash: UITableView dataSource must return a celliOS 5 Storyboard 自定义单元格崩溃:UITableView 数据源必须返回一个单元格
【发布时间】:2012-02-06 11:23:40
【问题描述】:

这要么是 XCode 错误,要么是我在这里遗漏了一条关键规则。

更新: - 这是 XCode/Storyboard 中的一个奇怪错误的可能性有多大?

情况:

  • iOS 5,故事板
  • 这是故事板设置:http://i.imgur.com/T5WyD.png
  • 另一个完整设置的截图:http://i.imgur.com/1tVuz.png
  • 具有自定义单元格的 TableViewController,单元格具有可重复使用的标识符“NewCell”
  • 在“cellForRowAtIndexPath”中我基本上有:

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NewCell"]; return cell;

  • 这会引发异常:

    Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'

我已经尝试过的事情:

  • 我从头开始设置了一个新项目 TabBarController,其中包含一个 TableViewController 和一个自定义单元,所有这些都连接到 Storyboard 中,并设置了一个可重用的单元标识符。使用与上面相同的代码,完美运行。我不知道为什么它不适用于上面的情节提要......

我感觉和我在那里建的树有关系,是一个TabBarController,加载一个NavigationController,加载一个TableViewController,提供几个item,一个被点击,加载另一个TableViewController,无法以某种方式使用自定义单元格。

重要: - 问题是情节提要应确保: UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NewCell"]; 永远不会返回 NIL(与没有 Storyboard/iOS4 不同)。但是,我的是零。而且我无法弄清楚到底发生了什么。

【问题讨论】:

    标签: iphone ios5 cell storyboard


    【解决方案1】:
    • 您是否仔细检查了单元原型标识符是“NewCell”?
    • Tabbar->navigationcontroller->tableview 的设置非常好。一世 我一直在使用这种布局。
    • 还要确认这里没有无用的运行时属性
      viewcontroller/tableview/cell。
    • 您的原型在那里看起来有点奇怪 - 您在单元格中添加了一个 UIButton 作为子视图?为什么?

    【讨论】:

    • a) 检查 b) 它实际上是 tabbar->navcontroller->tableviewcontroller->tableviewcontroller->tabview->customcell c) 不 d) 添加按钮和随机的东西只是为了看看是否有任何显示.. . 在此之前只有标签
    【解决方案2】:

    我总是像这样在故事板中使用自定义单元格:

    static NSString *CellIdentifier = @"Cell";
    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }
    return cell;
    

    确保行数/节数至少为 1。另一件要检查的事情是,您将 UITableViewController 设置为情节提要中的自定义类名称。

    【讨论】:

    【解决方案3】:

    要解决此问题,请在您的 dequeueReusableCellWithIdentifier 语句上方添加以下内容:

    static NSString *CellIdentifier = @"NewCell";
    

    确保标识符与 Storyboard 中的原型单元格匹配

    【讨论】:

    【解决方案4】:

    嘿嘿,我今天刚遇到这个问题,想出了几个可能的原因。要将 UITableView 链接为 Story board 中 ViewController 的子视图,请检查您是否已完成这些步骤。

    1. 在您的“ViewController.h”中,将<UITableViewDataSource> 添加到您的列表中 协议数

      @interface ViewController : UIViewController <UITableViewDataSource>

      您可能也想添加<UITableViewDelegate>,但我没有。

    2. 在您的“ViewController.m”中设置您的表格视图数据源 功能

      #pragma mark - 表格视图数据源
      
      
      - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
      {
          // Return the number of sections.
          return 1;
      }
      
      - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
      {
          // Return the number of rows in the section.
          return [yourCells count];
      }
      
      - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
      {
      
          NewCell *cell = (NewCell *)[tableView 
                                                 dequeueReusableCellWithIdentifier:@"NewCell"];
          if (cell == nil) {
              NSLog(@"Cell is NIL");
              cell = [[CustomCell alloc] 
                      initWithStyle:UITableViewCellStyleDefault 
                      reuseIdentifier:CellIdentifier];
          }
      
          return cell;
      
      }
    3. 在 Storyboard 中,连接 UITableView 的“数据源”和 “委托”对 ViewController 的引用。

    4. 在情节提要中,为原型单元指定标识符“NewCell”

    如果这些都完成了,我相信它应该可以工作。

    希望这会有所帮助 =)

    【讨论】:

      【解决方案5】:

      回答这个问题可能已经晚了,但我敢打赌this 会解决你所有的问题

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-08-19
        • 1970-01-01
        • 2012-07-27
        • 2011-10-09
        • 1970-01-01
        • 2013-09-24
        相关资源
        最近更新 更多