这要么是 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 ,请检查您是否已完成这些步骤。
在“ViewController.h”中,添加
<UITableViewDataSource>
到你的 list 协议(protocol)数@interfaceViewController:UIViewController
<UITableViewDataSource>
您可能需要添加
<UITableViewDelegate>
也一样,但我没有。在“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;
在 Storyboard 中,连接 UITableView 的“数据源”并 对 ViewController 的“委托(delegate)”引用。
- 在 Storyboard中,为原型(prototype)单元指定标识符“NewCell”
如果这些都完成了,我相信它应该有效。
希望这有帮助 =)
关于iphone - iOS 5 Storyboard自定义单元格崩溃 : UITableView dataSource must return a cell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8796669/