2

更新:似乎我设法识别了单元格,因此解决了原始问题。但是现在,剪断相同的代码,我收到以下错误:

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 错误。matchData 是一个包含核心数据结果的数组。我只是不知道为什么 [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) 

非常感谢你。

4

2 回答 2

2

您必须为 Nib/Storyboard 中的原型单元分配正确的单元标识符。

错误很明显:'无法使具有标识符 Cell 的单元出队 - 必须注册一个 nib 或一个类

于 2012-10-31T13:32:09.777 回答
2

代替:

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];

祝你今天过得愉快

于 2012-11-15T18:16:53.880 回答