Iphone Xcode4:由于未捕获异常而终止应用程序';n内部一致性异常';

Iphone Xcode4:由于未捕获异常而终止应用程序';n内部一致性异常';,iphone,objective-c,ios5,xcode4,Iphone,Objective C,Ios5,Xcode4,我在使用iphone模拟器的xcode4中遇到了这个异常: Terminating app due to uncaught exception 'NSInternalInconsistencyException' 如果在CellForRowatinex方法中没有返回任何对象(返回0),则可能会导致此问题。但我已经准备好归还牢房了 有人能看到导致此错误的原因吗?以下是一组屏幕截图: 控制器配置: 原型电池 注意单元: 故事板: 完全错误: **Assertion failure in -

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

Terminating app due to uncaught exception 'NSInternalInconsistencyException'
如果在CellForRowatinex方法中没有返回任何对象(返回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;
}

这个错误是什么意思?我该如何修复它?

看起来好像您从未创建过单元格。如果您从未分配和初始化过单元格,那么就没有要出列的单元格。 在//配置单元之前。。行您需要检查可重用单元是否已出列,如果未出列,请创建一个如下所示的单元:

if (cell == nil) {
  cell = [[UITableViewCell alloc]  initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

  // Configure common elements here

}

  // Now go on to configure specific elements for this row
在使用故事板时,可能没有正确设置原型单元属性。标识符必须与上述方法中的标识符匹配


请参阅此图片,了解在何处可以找到原型单元属性。本教程也可能对您有所帮助

您似乎从未创建过单元格。如果您从未分配和初始化过单元格,那么就没有要出列的单元格。 在//配置单元之前。。行您需要检查可重用单元是否已出列,如果未出列,请创建一个如下所示的单元:

if (cell == nil) {
  cell = [[UITableViewCell alloc]  initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

  // Configure common elements here

}

  // Now go on to configure specific elements for this row
在使用故事板时,可能没有正确设置原型单元属性。标识符必须与上述方法中的标识符匹配


请参阅此图片,了解在何处可以找到原型单元属性。本教程可能也会对您有所帮助

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

无论如何,请尝试:

NSLog(@"current cell: %@ -> %@", identifier, cell);
并检查dequeueReusableCellWithIdentifier:返回的内容


只需将
attentionCell
拖放到表视图中即可

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

无论如何,请尝试:

NSLog(@"current cell: %@ -> %@", identifier, cell);
并检查dequeueReusableCellWithIdentifier:返回的内容


只需将
attentionCell
拖放到表视图中即可

如果OP使用的是故事板,他可能使用的是原型单元格,在这种情况下,
dequeueReusableCellWithIdentifier:
将始终返回一些东西,如果您传递在故事板中设置的标识符。这对我来说是新闻。我没有使用故事板。是的,这是正确的Jesse,我有两个不同的原型单元,它们位于if-else循环中(attentionCell和plainCell)。因此,是的,单元已经创建。谢谢..我不知道还能是什么…@Chi至少去试试这个答案。没有其他原因导致单元格为零。如果OP使用的是故事板,他可能使用的是原型单元格,在这种情况下,
dequeueReusableCellWithIdentifier:
将始终返回一些内容,如果您传递在故事板中设置的标识符。这对我来说是新闻。我没有使用故事板。是的,这是正确的Jesse,我有两个不同的原型单元,它们位于if-else循环中(attentionCell和plainCell)。因此,是的,单元已经创建。谢谢..我不知道还能是什么…@Chi至少去试试这个答案。没有其他原因导致单元格为零。是的,换句话说,为其他重用标识符添加第二个原型单元格。是的,查看屏幕截图,我看到第二个原型单元格需要移动到tableview。你应该可以把它拖过来。第一次你需要制造一个“注意力”细胞。它肯定会失败的。嗯……谢谢卢帕图斯。它返回空值…所以我想它一定是我的配置?2012-07-30 19:20:30.302简单故事板[314:f803]当前单元格:plainCell->2012-07-30 19:20:30.305简单故事板[314:f803]当前单元格:attentionCell->(空)是否添加第二个原型单元格?我有两个原型细胞。你是说再加一个就三个?你能解释一下吗Jesse?是的,换句话说,为其他重用标识符添加第二个原型单元格。是的,看屏幕截图,我看到第二个原型单元格需要移动到tableview。你应该可以把它拖过来。第一次你需要制造一个“注意力”细胞。它肯定会失败的。嗯……谢谢卢帕图斯。它返回空值…所以我想它一定是我的配置?2012-07-30 19:20:30.302简单故事板[314:f803]当前单元格:plainCell->2012-07-30 19:20:30.305简单故事板[314:f803]当前单元格:attentionCell->(空)是否添加第二个原型单元格?我有两个原型细胞。你是说再加一个就三个?你能解释一下吗,杰西?