Ios TableView NSInternalInconsistencyException异常

Ios TableView NSInternalInconsistencyException异常,ios,objective-c,xcode,uitableview,cocoa-touch,Ios,Objective C,Xcode,Uitableview,Cocoa Touch,更新:看起来我成功地识别了电池,所以原来的问题解决了。但是现在,在截取相同的代码后,我得到了以下错误: 2012-10-31 15:21:41.857 Laktase[22658:11603] -[NSManagedObject isEqualToString:]: unrecognized selector sent to instance 0x74a5a30 2012-10-31 15:21:41.858 Laktase[22658:11603] *** Terminating app du

更新:看起来我成功地识别了电池,所以原来的问题解决了。但是现在,在截取相同的代码后,我得到了以下错误:

2012-10-31 15:21:41.857 Laktase[22658:11603] -[NSManagedObject isEqualToString:]: unrecognized selector sent to instance 0x74a5a30
2012-10-31 15:21:41.858 Laktase[22658:11603] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSManagedObject isEqualToString:]: unrecognized selector sent to instance 0x74a5a30'
*** First throw call stack:
(0x1f9f012 0x13dce7e 0x202a4bd 0x1f8ebbc 0x1f8e94e 0x1714af 0x171674 0x4430 0xd2f4b 0xd301f 0xbb80b 0xcc19b 0x6892d 0x13f06b0 0x259bfc0 0x259033c 0x259beaf 0x1078cd 0x501a6 0x4ecbf 0x4ebd9 0x4de34 0x4dc6e 0x4ea29 0x51922 0xfbfec 0x48bc4 0x48dbf 0x48f55 0x51f67 0x15fcc 0x16fab 0x28315 0x2924b 0x1acf8 0x1efadf9 0x1efaad0 0x1f14bf5 0x1f14962 0x1f45bb6 0x1f44f44 0x1f44e1b 0x167da 0x1865c 0x268d 0x25b5 0x1)
libc++abi.dylib: terminate called throwing an exception
(lldb) 
//更新结束

我第一次尝试查询数据库,然后在表中列出结果

当我将numberOfRowsInSection从0更改为[matchingData count]时,应用程序会崩溃,并出现NSInternalInconsistencyException错误。matchingData是一个包含核心数据结果的数组。我就是不明白为什么[matchingData count]不起作用

这是我的密码:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Tablette" inManagedObjectContext:context];
    NSFetchRequest *request = [NSFetchRequest new];
    [request setEntity:entity];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"dosis >= 0"]; // @"ANY Tablette.dosis like '1'"];
    [request setPredicate:predicate];
    NSError *error;
    NSArray *matchingData = [context executeFetchRequest:request error:&error];
    return [matchingData count];
}

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

// Configure the cell...


NSEntityDescription *entity = [NSEntityDescription entityForName:@"Tablette" inManagedObjectContext:context];
NSFetchRequest *request = [NSFetchRequest new];
[request setEntity:entity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"dosis >= 1"];
[request setPredicate:predicate];
NSError *error;
NSArray *matchingData = [context executeFetchRequest:request error:&error];

cell.textLabel.text = [matchingData objectAtIndex:indexPath.row];

return cell;
}
例外情况如下:

2012-10-31 12:10:38.995 Laktase[21797:11603] *** Assertion failure in -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:], /SourceCache/UIKit_Sim/UIKit-2372/UITableView.m:4460
2012-10-31 12:10:38.996 Laktase[21797:11603] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier Cell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
*** First throw call stack:
(0x1f9f012 0x13dce7e 0x1f9ee78 0xb66f35 0xc9d14 0x4078 0xd2f4b 0xd301f 0xbb80b 0xcc19b 0x6892d 0x13f06b0 0x259bfc0 0x259033c 0x259beaf 0x1078cd 0x501a6 0x4ecbf 0x4ebd9 0x4de34 0x4dc6e 0x4ea29 0x51922 0xfbfec 0x48bc4 0x48dbf 0x48f55 0x51f67 0x15fcc 0x16fab 0x28315 0x2924b 0x1acf8 0x1efadf9 0x1efaad0 0x1f14bf5 0x1f14962 0x1f45bb6 0x1f44f44 0x1f44e1b 0x167da 0x1865c 0x249d 0x23c5 0x1)
libc++abi.dylib: terminate called throwing an exception
(lldb) 

非常感谢。

您必须在Nib/情节提要中为原型单元分配正确的单元标识符


错误很明显:“无法将标识符为cell的单元出列-必须为

注册nib或类。必须在nib/情节提要中将正确的单元标识符分配给原型单元。”

错误很明显:“无法将标识符为cell的单元格出列-必须为

注册nib或类,而不是:

static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
请尝试以下操作:

static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
测试单元格是否为空非常重要,它总是发生在第节中配置的第一个单元格上

if (cell == nil)
{ //alloc the cell explicitily

    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:kCellID];
    .....
}

// Configure the cell.
[self configureCell:cell atIndexPath:indexPath];
祝你度过愉快的一天,而不是:

static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
请尝试以下操作:

static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
测试单元格是否为空非常重要,它总是发生在第节中配置的第一个单元格上

if (cell == nil)
{ //alloc the cell explicitily

    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:kCellID];
    .....
}

// Configure the cell.
[self configureCell:cell atIndexPath:indexPath];


祝您度过愉快的一天

您能给我们看一下实际的异常输出吗?Tablette对象的数量有变化吗?如果是这样,您需要确保调用
[UITableView reloadData]
,否则会导致数据源与显示内容不一致。感谢您的建议,很高兴知道,但是在这种情况下它不会改变。如果我是正确的,那似乎是因为nib中缺少可重用标识符。你能显示你的cellForRowAtIndexPath函数吗?你能给我们显示实际的异常输出吗?Tablette对象的数量有变吗?如果是这样,您需要确保调用
[UITableView reloadData]
,否则会导致数据源与显示内容不一致。感谢您的建议,很高兴知道,但是在这种情况下它没有改变。如果我是正确的,那似乎是因为nib中缺少可重用标识符。你能显示你的CellForRowatineXpath函数吗?不确定(我必须知道你的对象的确切数据结构),但我认为这行
cell.textLabel.text=[matchingData objectAtIndex:Indexath.row]
应类似于:
cell.textlab.text=[[matchingData objectAtIndex:indexPath.row]aMethodReturningText]
或类似内容。您必须指向NSString,现在您指向的是NSManagedObject实例OK,我将其更改为:cell.textLabel.text=[NSString stringWithFormat:@“%@,[matchingData objectAtIndex:Indexath.row]];应用程序没有启动,表格出现了,但我只能看到类似的行:“(ent…”这是什么?现在您正在打印对象的描述!!!!:-)我试图解释:在数组匹配数据中,您有一些NSManagedObject实例。您正试图在屏幕上打印某些内容。你想打印什么?名字?文本?好的,此名称/文本存储在NSManagedObject实例属性之一中。因此,要打印它,必须编写类似于
cell.textlab.text=[[matchingData objectAtIndex:indexath.row]propertyName]
的内容,其中propertyName是包含要显示的文本的属性的名称!粘贴您的对象。h属性列表:-)非常感谢您的解释!:-)我尝试了这行代码:cell.textlab.text=[[matchingData objectAtIndex:indexath.row]valueForKey:@“dosis”];现在数据库在表中,但是3次:-D这是怎么发生的?当我用[matchingData count]计数时,它显示3个匹配项,这是正确的,但表中显示9…:-)嗯…看清楚你的代码,我建议你重构它。每次tableview调用其数据源时,您都会发出一个NSFetchRequest(数据库中的每一行一次),另一次请求计数。我建议您在某些init方法中只执行一个fetch请求,并将所有数据放在一个数组中,或者更好地使用NSFetchedResultsController。您所描述的问题可能是因为您正在执行不同的请求以获得计数和对象(谓词不同)。不确定(我必须知道对象的确切数据结构),但我认为这行
cell.textlab.text=[matchingData objectAtIndex:indexPath.row]
应类似于:
cell.textlab.text=[[matchingData objectAtIndex:indexPath.row]aMethodReturningText]
或类似内容。您必须指向NSString,现在您指向的是NSManagedObject实例OK,我将其更改为:cell.textLabel.text=[NSString stringWithFormat:@“%@,[matchingData objectAtIndex:Indexath.row]];应用程序没有启动,表格出现了,但我只能看到类似的行:“(ent…”这是什么?现在您正在打印对象的描述!!!!:-)我试图解释:在数组匹配数据中,您有一些NSManagedObject实例。您正试图在屏幕上打印某些内容。你想打印什么?名字?文本?好的,此名称/文本存储在NSManagedObject实例属性之一中。因此,要打印它,必须编写类似于
cell.textlab.text=[[matchingData objectAtIndex:indexath.row]propertyName]
的内容,其中propertyName是包含要显示的文本的属性的名称!粘贴您的对象。h属性列表:-)非常感谢您的解释!:-)我尝试了这行代码:cell.textlab.text=[[matchingData objectAtIndex:indexath.row]valueForKey:@“dosis”];现在数据库在表中,但是3次:-D这是怎么发生的?当我用[matchingData count]计数时,它显示3个匹配项,这是正确的,但表中显示9…:-)M