iOS 5 Storyboard自定义单元格崩溃:UITableView dataSource必须返回一个单元格
这是一个XCode错误,或者我在这里错过了一个关键规则。
This is either an XCode bug, or me missing a crucial rule here.
更新:
- 这在XCode / Storyboard中出现奇怪错误的可能性是多少?
Update: - What's the chance of this being a weird bug in XCode/Storyboard?
情况:
- iOS 5,故事板
- 这是故事板设置: http://i.imgur.com/T5WyD.png
- 完整设置的另一个屏幕截图: http:// i .imgur.com / 1tVuz.png
- TableViewController与自定义单元格,单元格具有可重复使用的标识符NewCell
-
in cellForRowAtIndexPath我基本上有:
- iOS 5, Storyboard
- This is the storyboard setup: http://i.imgur.com/T5WyD.png
- Another screenshot of the full setup: http://i.imgur.com/1tVuz.png
- TableViewController with Custom Cell, cell has reusable identifier "NewCell"
in "cellForRowAtIndexPath" I basically have:
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@NewCell];
返回单元格;
这会引发异常:
由于未捕获的异常'NSInternalInconsistencyException'终止应用程序,原因:'UITableView dataSource必须从tableView返回一个单元格:cellForRowAtIndexPath:'
我已尝试过的事情:
- I从头开始设置一个新项目,TabBarController,一个TableViewController和一个Custom Cell,都在Storyboard中连接,设置一个可重用的单元标识符。使用与上面相同的代码,完美地工作。我不知道为什么它不能与上面的故事板一起工作...
- I setup a new project from scratch, TabBarController, with a TableViewController and a Custom Cell, all wired up in Storyboard, set a reusable cell identifier. Used the same code as above, worked perfectly. I don't know why it didn't work with the storyboard above...
我觉得它有事情要做使用我在那里构建的树,这是一个TabBarController,加载一个NavigationController,加载一个TableViewController,提供一些项目,一个被点击,加载另一个TableViewController,无法以某种方式使用自定义单元格。
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.
重要:
- 问题是故事板应该确保: UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@NewCell];
永远不会返回NIL(与没有Storyboard / iOS4不同)。但是,我的是零。我不能,为了它的地狱,弄清楚发生了什么。
Important:
- The issue is that the Storyboard should make sure that:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NewCell"];
will never return NIL (unlike without the Storyboard/iOS4). But, mine is nil. And I can't, for the hell of it, figure out what's happening.
Heyo,我今天刚遇到这个问题并找出了一些可能的原因。要将UITableView链接为Story Board中ViewController的子视图,请检查您是否已完成这些步骤。
Heyo, I just had this problem today and figured out a few possible causes. To link a UITableView as a subview of a ViewController in Story board check that you have done these steps.
-
在ViewController中.h,将
< UITableViewDataSource>
添加到您的列表
协议
In your "ViewController.h", add
<UITableViewDataSource>
to your list of protocols
@interface ViewController: UIViewController < UITableViewDataSource>
@interface ViewController : UIViewController
<UITableViewDataSource>
您可能想要添加< UITableViewDelegate>
以及我没有。
you might want to add <UITableViewDelegate>
as well but I didn't.
在ViewController.m中设置表格视图数据源
函数
In your "ViewController.m" set up your Table view data source functions
#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。
In Storyboard, connect the UITableView's "Data Source" and "Delegate" references to the ViewController.
如果完成这些操作,我相信它应该可行。
If these are done, I believe it should work.
希望这有助于=)