Xcode4:终止应用程序由于未捕获的异常“NSInternalInconsistencyException”
我得到这个例外在xcode4与iPhone模拟器:如果你没有返回对象Xcode4:终止应用程序由于未捕获的异常“NSInternalInconsistencyException”
Terminating app due to uncaught exception 'NSInternalInconsistencyException'
这似乎引起了(返回0)回cellForRowAtIndex方法。但我有我的设置返回细胞。
任何人都可以看到什么可能会导致此错误?下面是一串截图:
控制器配置:
原型细胞
attentionCell:
故事板:
完全错误:
**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
当您使用的故事板,也许你还没有设置原型单元属性正确。标识符必须与上述方法中的标识符相匹配。
请参阅此图片以查找原型单元格属性Prototype cell setup。它是从这个教程,也可以帮助你Ray Wenderlich and Storyboards
我想你的第二个原型单元格必须是你的tableView在故事板的一部分,现在它只是同一场景的一部分 - 可能这就是为什么你'重新获得零。
反正尝试:
NSLog(@"current cell: %@ -> %@", identifier, cell);
和检查什么dequeueReusableCellWithIdentifier:
正在恢复。
只需将如果OP是用故事板,他可能会使用的原型细胞下降到attentionCell
表视图
是的,换句话说,为其他重用标识符添加第二个原型单元格。 – 2012-07-31 00:14:36
是的,看着屏幕截图我看到第二架原型机单元需要被移动到的tableview。你应该可以拖动它。您第一次需要制作“关注”单元格。它肯定会失败。 – 2012-07-31 00:15:10
嗯..感谢Lupatus。它回来了......所以它一定是我的配置,那么我想呢? 2012-07-30 19:20:30.302简单故事板[314:F803]当前小区:plainCell - >> 2012-07-30 19:20:30.305简单故事板[314:F803]当前小区:attentionCell - >(空) – SaintClaire33 2012-07-31 00:23:41
,在这种情况下'dequeueReusableCellWithIdentifier:'总是返回的东西,如果你是传递你在故事板中设置的标识符。 – 2012-07-30 20:13:47
这对我来说是新闻。我没有使用故事板。 – 2012-07-30 20:16:01
是的,这是正确的杰西我有两个不同的原型单元格,如果else(loopCell和plainCell)的循环。 – SaintClaire33 2012-07-30 20:26:10