【问题标题】:UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:UITableView dataSource 必须从 tableView:cellForRowAtIndexPath 返回一个单元格:
【发布时间】:2011-08-19 18:49:40
【问题描述】:

我不断收到这个错误,我已经设置了一个我试图显示的自定义单元格我已经在 IB 中连接了数据源和委托,但是我不断收到这个错误,下面是我的代码......就像我一样疯狂已经在另一个项目中完成了这项工作,而且效果很好。除了变量名之外,我没有更改任何内容。

2011-05-06 10:40:17.004 instaCode1.3[1500:207] * 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“UITableView 数据源必须从 tableView 返回一个单元格:cellForRowAtIndexPath:'

一切都被@synthesized 等等...

#pragma mark -
#pragma mark Table view data source
// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

//This method adds headings to my sections
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    NSString *title = nil;
    // Return a title or nil as appropriate for the section.
    switch (section) {
        case REG_SECTION:
            title = @"Enter Registration";
            break;
        default:
            break;
    }
    return title;;
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    if (indexPath.section == 0) {

        return cellRegistration;            

    }
    return 0;
}

【问题讨论】:

  • cellRegistration 为零吗?
  • 我现在已经为零了..但它仍然无法正常工作..我只是不知道我做错了什么。
  • 您不希望 cellRegistration 为 nil。这是您要返回的单元格。
  • 如何将其初始化为我在 Interfacebuilder 中创建的自定义单元格?
  • 这完全是另一个问题。 stackoverflow.com/questions/540345/…

标签: iphone ios uitableview datasource


【解决方案1】:

cellForRowAtIndexPath: 函数返回一个 UITableViewCell 对象,因此 0 不是有效的返回选项。您必须返回一个单元格。

【讨论】:

    【解决方案2】:

    如果您有自定义单元格,则必须检查该单元格的注册。这在任何情况下都不应该为零。

    如果它为 nil,那么您可以使用 ALLOC、INIT 来获取单元注册并返回。

    【讨论】:

      【解决方案3】:

      我认为您是在为 cellforRowAtIndexPath 中的“返回 0”找麻烦。 我会在该行放置一个断点,看看它是否是中断的地方,如果是,为什么节 != 0?

      REG_SECTION == 0(你确定吗?)

      编辑: 这在日志中为您提供了什么?

          if (indexPath.section == 0) {
              NSLog(@" cellRegistration is %@",cellRegistration);
              return cellRegistration;            
          }
      

      【讨论】:

      • 是的,但是如果没有返回 0,它会抛出“控制到达非 void 函数的结尾”......男人只是迷失了这一点,因为我以前做过同样的事情......
      • 在这种情况下,您希望返回 nil,而不是 0。
      • 哦,谢谢。不幸的是,它仍然给我同样的错误......我想我可能只是重新创建文件,看看是否能解决问题......太奇怪了......
      • 您是否尝试过所有常见的操作,例如清理所有目标、重置模拟器内容、关闭/重新打开项目等?
      • 只需初始化cellRegistration。因为它是零。或者用有意义的东西替换整个东西。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-09
      • 2013-09-24
      • 2012-07-27
      • 2012-03-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多