简体   繁体   English

iOS 5故事板自定义单元崩溃:UITableView dataSource必须返回单元

[英]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: 情况:

  • iOS 5, Storyboard iOS 5,情节提要
  • This is the storyboard setup: http://i.imgur.com/T5WyD.png 这是情节提要板设置: http : //i.imgur.com/T5WyD.png
  • Another screenshot of the full setup: http://i.imgur.com/1tVuz.png 完整设置的另一个屏幕截图: http : //i.imgur.com/1tVuz.png
  • TableViewController with Custom Cell, cell has reusable identifier "NewCell" 具有自定义单元格的TableViewController,单元格具有可重复使用的标识符“ NewCell”
  • 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 setup a new project from scratch, TabBarController, with a TableViewController and a Custom Cell, all wired up in Storyboard, set a reusable cell identifier. 我从头开始设置了一个新项目TabBarController,其中一个TableViewController和一个Custom Cell都连接在Storyboard中,并设置了可重用的单元格标识符。 Used the same code as above, worked perfectly . 使用与上面相同的代码, 效果很好 I don't know why it didn't work with the storyboard above... 我不知道为什么它不适用于上面的故事板...

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的子视图,请检查是否已完成以下步骤。

  1. 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> ,但是我没有。

  2. In your "ViewController.m" set up your Table view data source functions 在“ ViewController.m”中设置表视图数据源功能

     #pragma mark - Table view data source #pragma mark-表格视图数据源\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} }\n\n
  3. In Storyboard, connect the UITableView's "Data Source" and "Delegate" references to the ViewController. 在情节提要中,将UITableView的“数据源”和“代理”引用连接到ViewController。

  4. In Storyboard, give the Prototype Cell the Identifier "NewCell" 在情节提要中,为原型单元赋予标识符“ NewCell”

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设置为自定义类名称。

回答这个问题可能为时已晚,但我敢打赌, 将解决您的所有问题

  • Did you double check the cell prototype identifier is "NewCell"? 您是否再次检查了单元原型的标识符为“ NewCell”?
  • Your setup as Tabbar->navigationcontroller->tableview is perfectly fine. 将您的设置设置为Tabbar-> navigationcontroller-> tableview非常好。 I am using this layout all the time. 我一直在使用这种布局。
  • verify also there's no useless runtime attributes anywhere in this 验证在此任何地方都没有无用的运行时属性
    viewcontroller/tableview/cell. viewcontroller / tableview / cell。
  • your prototype looks a bit weird there - you added a UIButton to the cell as subview? 您的原型看起来有点奇怪-您在单元格中添加了一个UIButton作为子视图? why? 为什么?

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.

相关问题 UITableView dataSource必须返回一个单元格 - UITableView dataSource must return a cell 得到警告:UITableView数据源必须返回一个单元格 - get warning :UITableView dataSource must return a cell UITableView dataSource必须从tableView:cellForRowAtIndexPath返回一个单元格 - UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath 'UITableView dataSource 必须从 tableView 返回一个单元格:cellForRowAtIndexPath:' - 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:' UITableView数据源必须从tableView:cellForRowAtIndexPath返回一个单元格:异常 - UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath: Exception 原因:“ UITableView数据源必须从tableView:cellForRowAtIndexPath返回单元格 - reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath UITableView数据源必须从tableView:cellForRowAtIndexPath返回一个单元格: - UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath: 使用UITableViewCellStyleDefault在iOS 7中未拾取情节提要UITableView中的自定义单元格 - Custom Cell in storyboard UITableView not being picked up in ios7 with UITableViewCellStyleDefault 具有自定义单元StoryBoard的iOS SearchDisplayController - IOS SearchDisplayController with Custom Cell StoryBoard UITableview不更新情节提要中的uitableviewcell自定义单元格 - UITableview not updating uitableviewcell custom cell from storyboard
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM