[英]iOS 5 Storyboard Custom Cell Crash: UITableView dataSource must return a cell
This is either an XCode bug, or me missing a crucial rule here. 这是XCode错误,或者我在这里错过了关键规则。
Update: - What's the chance of this being a weird bug in XCode/Storyboard? 更新: -这是XCode / Storyboard中一个奇怪的错误的可能性有多大?
Situation: 情况:
in "cellForRowAtIndexPath" I basically have: 在“ cellForRowAtIndexPath”中,我基本上有:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NewCell"]; return cell;
This throws an exception: 这将引发异常:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'
Things I have already tried: 我已经尝试过的事情:
I have a feeling it has something to do with the tree I built there, which is a TabBarController, loading a NavigationController, loading a TableViewController, providing a few items, one is clicked, which loads another TableViewController, which is unable to work with the custom cell, somehow. 我有一种感觉,它与我在其中建立的树有关,这是一个TabBarController,加载一个NavigationController,加载一个TableViewController,提供一些项目,单击其中的一个,加载另一个TableViewController,它无法使用自定义单元,以某种方式。
Important: - The issue is that the Storyboard should make sure that: UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NewCell"];
重要说明: -问题是情节提要板应确保:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NewCell"];
will never return NIL (unlike without the Storyboard/iOS4). 永远不会返回NIL(与没有Storyboard / iOS4的情况不同)。 But, mine is nil.
但是,我的为零。 And I can't, for the hell of it, figure out what's happening.
而且我无法理解到底发生了什么。
Heyo, I just had this problem today and figured out a few possible causes. Heyo,我今天才遇到这个问题,并找出了一些可能的原因。 To link a UITableView as a subview of a ViewController in Story board check that you have done these steps.
要将UITableView链接为故事板中ViewController的子视图,请检查是否已完成以下步骤。
In your "ViewController.h", add <UITableViewDataSource>
to your list of protocols 在“ ViewController.h”中,将
<UITableViewDataSource>
添加到协议列表中
@interface ViewController : UIViewController <UITableViewDataSource>
@interface ViewController:UIViewController
<UITableViewDataSource>
you might want to add <UITableViewDelegate>
as well but I didn't. 您可能也想添加
<UITableViewDelegate>
,但是我没有。
In your "ViewController.m" set up your Table view data source functions 在“ ViewController.m”中设置表视图数据源功能
#pragma mark - Table view data source#pragma mark-表格视图数据源\n\n
\n\n}- (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;
}\n\n
In Storyboard, connect the UITableView's "Data Source" and "Delegate" references to the ViewController. 在情节提要中,将UITableView的“数据源”和“代理”引用连接到ViewController。
If these are done, I believe it should work. 如果这些都完成了,我相信它应该起作用。
Hope this helps =) 希望这可以帮助=)
I always use custom cells in the storyboard like this: 我总是在情节提要中使用自定义单元格,如下所示:
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
return cell;
Make sure the number of rows/sections is at least one. 确保行/节的数量至少为一。 And another thing to check is that you set the UITableViewController to your custom class name in the storyboard.
还要检查的另一件事是,在情节提要中将UITableViewController设置为自定义类名称。
回答这个问题可能为时已晚,但我敢打赌, 这将解决您的所有问题
To solve this problem add the following just above your dequeueReusableCellWithIdentifier statement: 要解决此问题,请在您的dequeueReusableCellWithIdentifier语句上方添加以下内容:
static NSString *CellIdentifier = @"NewCell";
making sure the identifier matches the prototype cell in Storyboard 确保标识符与情节提要中的原型单元格匹配
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.