iphone - iOS 5 Storyboard自定义单元格崩溃 : UITableView dataSource must return a cell

标签 iphone ios5 cell storyboard

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

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

情况:

  • iOS 5、 Storyboard
  • 这是 Storyboard设置:/image/xQBZk.png
  • 完整设置的另一个屏幕截图:/image/xDbuW.png
  • 带有自定义单元格的TableViewController,单元格具有可重用标识符“NewCell”
  • 在“cellForRowAtIndexPath”中我基本上有:

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NewCell"]; 返回单元格;

  • 这会引发异常:

    由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“UITableView dataSource 必须从 tableView:cellForRowAtIndexPath: 返回一个单元格:”

我已经尝试过的事情:

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

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

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

最佳答案

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

  1. 在“ViewController.h”中,添加<UITableViewDataSource>到你的 list 协议(protocol)数

    @interfaceViewController:UIViewController <UITableViewDataSource>

    您可能需要添加 <UITableViewDelegate>也一样,但我没有。

  2. 在“ViewController.m”中设置 TableView 数据源 功能

    #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 的“数据源”并 对 ViewController 的“委托(delegate)”引用。

  4. 在 Storyboard中,为原型(prototype)单元指定标识符“NewCell”

如果这些都完成了,我相信它应该有效。

希望这有帮助 =)

关于iphone - iOS 5 Storyboard自定义单元格崩溃 : UITableView dataSource must return a cell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8796669/

相关文章:

iphone - tableviewcell 中的图像减慢滚动速度

ios - 自定义 UITableViewController 结构

iphone - JSON 框架没有隐藏的 API?

iphone - objective-c 中导航栏标题中的图像

iphone - UILabel 无法更新标签文本

ios - 获取链接并消除所有困惑并仅关注内容 - 在 Objective-C 中执行此操作的最佳方法是什么?

ios - 在 Swift 中保留 UITableView 中的单元格?

Swift:如何从动态单元捕获开关的修改

swing - JTable : how to get selected cells?

ios - 颠倒呈现 UIActivityViewController