iOS 5 Storyboard自定义单元格崩溃:UITableView dataSource必须返回一个单元格

时间:2012-01-09 23:29:22

标签: iphone ios5 cell storyboard

这是一个XCode错误,或者我在这里错过了一个关键规则。

更新 - 这在XCode / Storyboard中成为一个奇怪的错误的可能性是什么?

情况:

  • iOS 5,故事板
  • 这是故事板设置:http://i.imgur.com/T5WyD.png
  • 完整设置的另一个屏幕截图:http://i.imgur.com/1tVuz.png
  • TableViewController with Custom Cell,cell具有可重用的标识符“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和一个Custom Cell,都在Storyboard中连接,设置一个可重用的单元标识符。使用与上面相同的代码,完美地工作。我不知道为什么它不能与上面的故事板一起工作......

我感觉它与我在那里构建的树有关,这是一个TabBarController,加载一个NavigationController,加载一个TableViewController,提供一些项目,一个被点击,它加载另一个TableViewController,它无法以某种方式使用自定义单元格。

重要:    - 问题是故事板应该确保:     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NewCell"];永远不会返回NIL(与没有Storyboard / iOS4不同)。但是,我的是零。我不能,为了它的原因,弄清楚发生了什么。

5 个答案:

答案 0 :(得分:2)

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

  1. 在您的&#34; ViewController.h&#34;中,将<UITableViewDataSource>添加到您的列表中 协议

    @interface ViewController:UIViewController <UITableViewDataSource>

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

  2. 在你的&#34; ViewController.m&#34;设置表视图数据源 功能

    #pragma mark - Table view data source
    
    
    - (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&#34;数据源&#34;和 &#34;代表&#34;对ViewController的引用。

  4. 在Storyboard中,为Prototype Cell提供标识符&#34; NewCell&#34;
  5. 如果这些已经完成,我认为它应该有效。

    希望这有助于=)

答案 1 :(得分:1)

我总是在故事板中使用自定义单元格,如下所示:

static NSString *CellIdentifier = @"Cell";

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

确保行/部分的数量至少为一个。另一件要检查的是你在故事板中将UITableViewController设置为自定义类名。

答案 2 :(得分:0)

  • 您是否仔细检查过单元格原型标识符是“NewCell”?
  • 您的设置为Tabbar-&gt; navigationcontroller-&gt; tableview完全没问题。一世 我一直在使用这种布局。
  • 验证此处的任何地方都没有无用的运行时属性 视图控制器/的tableview /细胞。
  • 你的原型在那里看起来有点奇怪 - 你在单元格中添加了一个UIButton作为子视图?为什么呢?

答案 3 :(得分:0)

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

static NSString *CellIdentifier = @"NewCell";

确保标识符与Storyboard

中的原型单元格匹配

答案 4 :(得分:0)

要回答这个问题可能会迟到,但我打赌this会解决所有问题