从tableView:cellForRowAtIndexPath返回NSInternalInconsistencyException:使用Storyboard时

时间:2012-09-11 00:58:43

标签: iphone objective-c ipad ios5

我对Objective C和iOS开发相当新,而且我仍然想要了解它。但我正在尝试使用故事板制作一个简单的iOS应用程序(截至目前)UITableViewController作为主页面。我只是想让细胞出现。我的代码是:

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath    *)indexPath
{
    static NSString *cervical_patterns = @"cervical_patterns";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cervical_patterns"];

   return cell;
}

我已经密切关注了第二个iOS观鸟教程应用(link),并且之前已经获得了一个可在模拟器中查看的UITableViewController。但我无法弄清楚为什么这不像观鸟应用程序那样。

我得到的错误是:

'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:

我做错了什么?

2 个答案:

答案 0 :(得分:0)

所以你使用的是Storyboard和UITableViewController。这意味着你需要:

  1. 转到您的故事板
  2. 点击表格视图中的第一个单元格
  3. 确保显示实用程序(右侧窗口)
  4. 单击“属性检查器”
  5. 改变"风格"来自" Custom"到"基本" (如果您已自定义单元格,请使用自定义)
  6. 单击Identifier并确保它与cellForRowAtIndexPath方法(cervical_patterns)相同
  7. 同时检查您是否正确设置了课程:

    1. 转到您的故事板
    2. 点击表视图下方的黑条
    3. 确保显示实用程序(右侧窗口)
    4. 单击Identity Inspector
    5. 确保在下拉列表中选择了自定义类
    6. 我刚刚在XCode中完成了这项工作并且完美运行(请注意,没有 (cell == nil)代码是必需的)。下面的确切代码:

      - (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 1;
      }
      
      - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
      {
          static NSString *CellIdentifier = @"cervical_patterns";
          UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
      
          // Configure the text and graphics of the cell
      
          return cell;
      }
      

答案 1 :(得分:-1)

您需要实际创建单元格,如下所示:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath    *)indexPath
{
static NSString *cervical_patterns = @"cervical_patterns";
UITableViewCell *cell = [tableView         dequeueReusableCellWithIdentifier:@"cervical_patterns"];

    if (cell == nil) {
       cell = [[[UITableViewCell alloc]
               initWithStyle:UITableViewCellStyleDefault
             reuseIdentifier:cervical_patterns ] autorelease];

    }

   return cell;
}