【问题标题】:cellForRowAtIndexPath: returns nil for a generic prototype cellcellForRowAtIndexPath:为通用原型单元返回 nil
【发布时间】:2012-01-31 22:57:01
【问题描述】:

这让我发疯了!

我有一个带有通用原型单元格的通用 UITableViewController 类,标识符为“myCell”。在ARC、iOS5下开发,使用Storyboard,我使用的方法如下:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myCell"];
    cell.textLabel.text = @"Title";
    return cell;
}

一切都与故事板、文件所有者等相关联。我的项目中还有其他几个 UITableViewController,使用相同的概念。所有人都在工作。 这个特殊的 UITableViewController 类不想工作!继续向我抛出异常:

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

Nslog'ed --> [tableView dequeueReusableCellWithIdentifier:@"myCell"] = (null)

非常感谢任何想法和帮助!

编辑:

为简单起见: - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 返回 1; }

我已经做过的事情: 1.在storyboard中删除并创建和设置UITableViewController! 2.重启Xcode 3.重启模拟器 4. 重启电脑!!!! 5.一遍又一遍地删除和构建应用程序! 没有任何效果。

顺便说一下,我试过了

if (cell == nil)
 cell = [UITableViewCell alloc] initWithStyle....

.. 它会解决问题,但同样...这是 ARC,故事板,...我不应该那样做。 Xcode 应该会处理它!

【问题讨论】:

  • 我也有同样的问题。你能告诉你是如何解决这个问题的吗?我必须错过故事板配置中的某些内容。
  • 还没弄明白。如果我找到答案,我会在这里发布。
  • 看起来我们在故事板中遗漏了一些东西。它可能有一些我们需要更改的默认行为。我想知道有多少人有同样的问题......

标签: xcode4 uitableview storyboard automatic-ref-counting


【解决方案1】:

你必须创建你的细胞:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myCell"];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"myCell"] autorelease];
    }

    cell.textLabel.text = @"Title";
    return cell;
}

【讨论】:

  • 这是ARC下的,不需要alloc/init/autorelease。故事板应该这样做,就像在我的所有其他 UITableViewController 实例中一样。
【解决方案2】:

最终我发现了这个错误。我不能说我找到了根本原因,但逐行调查,这是我发现并为我工作的。

我的UITableViewController 中有一些对象需要在加载视图之前进行分配/初始化,以便调用者可以将它们设置为预先确定的值。由于viewDidLoad太晚了,我把它们放在initWithCoder方法中。

退出并重写initwithCoder 方法解决了这个问题。在我看来,initWithCoder 方法在初始化 UITableViewController 时有些不同!

【讨论】:

    【解决方案3】:

    我遇到了同样的问题,只是设法克服了它。 如果您要动态创建单元格数据,则必须执行以下操作:

    1. 选择表格视图。
    2. 然后转到对象检查器的右侧第三个选项卡。
    3. 第一个选项被命名为:“内容”。将选择从“Static Cells”更改为“Dynamic Prototypes”。
    4. 确保正确设置单元格的标识符,并且它与您在“dequeueResableCellWithIdentifier”中使用的名称相同

    它对我有用。我停止获取“nil”单元格值,一切似乎都正常工作。

    【讨论】:

      【解决方案4】:

      如果“IF”语句在以下情况下为真(单元格 == nil):

      - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
      {
          UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myCell"];
          if (cell == nil) {
              cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"myCell"] autorelease];
          }
          cell.textLabel.text = @"Title";
          return cell;
      }
      

      那么名称@"myCell" 拼写错误(不匹配)或单元格的情节提要标识符字段中缺失。

      【讨论】:

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