【问题标题】:Xcode4: Terminating app due to uncaught exception 'NSInternalInconsistencyException'Xcode4:由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序
【发布时间】:2012-07-28 13:02:13
【问题描述】:

我在使用 iphone 模拟器的 xcode4 中遇到了这个异常:

Terminating app due to uncaught exception 'NSInternalInconsistencyException'

这似乎是由于在 cellForRowAtIndex 方法中没有返回任何对象(返回 0)造成的。但是我已经设置好返回单元格了。

任何人都可以看到可能导致此错误的原因吗?这是一堆截图:

控制器配置:

原型细胞

注意单元:

故事板:

完全错误:

**Assertion failure in -[UITableView 
_createPreparedCellForGlobalRow:withIndexPath:],           
/SourceCache/UIKit_Sim/UIKit-1914.84/UITableView.m:6061
2012-07-30 14:34:16.660 Simple Storyboard[263:f803] Terminating app due 
to uncaught exception 'NSInternalInconsistencyException', 
reason: 'UITableView dataSource must return a cell from 
tableView:cellForRowAtIndexPath:'**

代码如下:

#import "LWWBidTaskListController.h"

@interface LWWBidTaskListController ()
@property (strong, nonatomic)NSArray *tasks;

@end

@implementation LWWBidTaskListController

@synthesize tasks;

- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
    // Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];

self.tasks = [NSArray arrayWithObjects: @"Walk the dog", @"URGENT:Buy milk", @"Clean     hidden lair", @"Invent miniature dolphins", @"Find new henchmen", @"Get revenge on do-gooder heroes", @"URGENT: Fold laundry", @"Hold entire hold hostage", @"Manicure", nil];

// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;

// Uncomment the following line to display an Edit button in the navigation bar for this     view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
self.tasks = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:    (UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
//#warning Potentially incomplete method implementation.
// Return the number of sections.
//return 0;
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//#warning Incomplete method implementation.
// Return the number of rows in the section.
//return 0;
return [tasks count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:    (NSIndexPath *)indexPath//populates each cell
{
//static NSString *CellIdentifier = @"Cell";
//UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];


NSString *identifier = nil;
NSString *task = [self.tasks objectAtIndex:indexPath.row];
NSRange urgentRange = [task rangeOfString:@"URGENT"];
if (urgentRange.location == NSNotFound)
{
    identifier = @"plainCell";
}
else 
{
    identifier = @"attentionCell";
}
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

// Configure the cell...

UILabel *cellLabel = (UILabel *)[cell viewWithTag:1];
cellLabel.text = task;

return cell;
}

这个错误是什么意思,我该如何解决?

【问题讨论】:

    标签: iphone objective-c ios5 xcode4


    【解决方案1】:

    看起来好像您从未创建单元格。如果您从未分配和初始化单元格,则没有可出列的单元格。 在 // 配置单元格.. 行之前,您需要检查一个可重复使用的单元格是否已出列,如果没有,请像这样创建一个:

    if (cell == nil) {
      cell = [[UITableViewCell alloc]  initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    
      // Configure common elements here
    
    }
    
      // Now go on to configure specific elements for this row
    

    当您使用故事板时,可能您没有正确设置原型单元格属性。标识符必须与上述方法中的标识符相匹配。

    请参阅这张图片,了解在哪里可以找到原型单元格属性 Prototype cell setup。本教程也可能对您有所帮助Ray Wenderlich and Storyboards

    【讨论】:

    • 如果 OP 正在使用情节提要,他可能正在使用原型单元,在这种情况下,如果您传递在情节提要中设置的标识符,dequeueReusableCellWithIdentifier: 将始终返回一些内容。
    • 这对我来说是个新闻。我没有使用故事板。
    • 是的,没错,杰西,我有两个不同的原型单元,它们位于 if else 循环中(attentionCell 和 plainCell)。
    • 所以是的,单元格已经创建,Dean。谢谢..我不确定它还能是什么......
    • @Chi 至少去试试这个答案。没有其他原因该单元格将为零。
    【解决方案2】:

    我想你的第二个原型单元必须是故事板中 tableView 的一部分,现在它只是同一场景的一部分 - 可能这就是你得到 nil 的原因。

    无论如何尝试:

    NSLog(@"current cell: %@ -> %@", identifier, cell);
    

    并检查 dequeueReusableCellWithIdentifier: 返回的内容。


    只需将attentionCell 拖放到表格视图中

    【讨论】:

    • 是的,换句话说,为另一个重用标识符添加第二个原型单元。
    • 是的,看屏幕截图我看到第二个原型单元格需要移动到表格视图中。你应该可以把它拖过来。第一次你需要产生一个“注意”单元。它肯定会失败。
    • 嗯..谢谢卢帕图斯。它回来为空......所以我想它一定是我的配置? 2012-07-30 19:20:30.302 简单故事板[314:f803] 当前单元格:plainCell -> > 2012-07-30 19:20:30.305 Simple Storyboard[314:f803] 当前单元格:attentionCell -> (null)
    • 添加第二个原型单元?我有两个原型细胞。你的意思是再加一个变成三个?你能解释一下那个杰西吗?
    猜你喜欢
    • 2014-01-05
    • 2015-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-08
    • 2021-01-29
    相关资源
    最近更新 更多