几天来,我一直在尝试使用 fecthedResultsController 将我的数据库信息(核心数据)传输到 tableview,但没有任何接缝可以工作!
我的实体名称是“密码”,有 4 个字符串属性。我正在尝试制作一个表格,其部分由一个名为“类型”的属性定义。现在,这就是我在 mainViewController.m 上的代码的样子:
-(NSFetchedResultsController*)fecthedResultsController
{
if(fecthedResultsController!=nil)
return fecthedResultsController;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Password" inManagedObjectContext:[self manageObjectContext]];
[fetchRequest setEntity:entity];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"type" ascending:YES];
//here is the problam
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
fecthedResultsController=[[NSFetchedResultsController alloc]initWithFetchRequest:fetchRequest managedObjectContext:[self manageObjectContext] sectionNameKeyPath:@"type" cacheName:nil];
fecthedResultsController.delegate=self;
NSLog(@"fecthedResultsController");
return fecthedResultsController;
}
-(void)controllerWillChangeContent:(NSFetchedResultsController *)controller
{
[self.tblMain beginUpdates];
}
-(void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
[self.tblMain endUpdates];
}
-(void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath
{
UITableView*tableview=self.tblMain;
NSLog(@"didChangeObject");
switch (type) {
case NSFetchedResultsChangeInsert:
[tableview insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]
withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:[tableview deleteRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]
withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeUpdate:{
Password*p=[self.fecthedResultsController objectAtIndexPath:indexPath];
UITableViewCell*cell=[tableview cellForRowAtIndexPath:indexPath];
cell.textLabel.text=p.userName;
cell.detailTextLabel.text=p.password;
// cell.imageView.image=[UIImage imageWithData: p.photodata];
}
break;
case NSFetchedResultsChangeMove:
[tableview deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
[tableview insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]
withRowAnimation:UITableViewRowAnimationFade];
break;
default:
break; }
}
-(void)controller:(NSFetchedResultsController *)controller didChangeSection:(id<NSFetchedResultsSectionInfo>)sectionInfo atIndex:(NSUInteger) sectionIndex forChangeType: (NSFetchedResultsChangeType)type
{
NSLog(@"didChangeSection");
switch (type) {
case NSFetchedResultsChangeInsert:
[self.tblMain insertSections:[NSIndexSet indexSetWithIndex:sectionIndex]
withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:[self.tblMain deleteSections:[NSIndexSet
indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
default:
break;
}
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return [[self.fecthedResultsController sections]count];
// NSLog(@"%i",[[self.fecthedResultsController sections]count]);
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
id<NSFetchedResultsSectionInfo>secInfo=[[self.fecthedResultsController sections]objectAtIndex:section];
return [secInfo numberOfObjects];
NSLog(@"numberOfRowsInSection");
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
Password*p=[self.fecthedResultsController objectAtIndexPath:indexPath];
cell.textLabel.text=p.userName;
// if(p.photodata!=nil)
// cell.imageView.image=[UIImage imageWithData: p.photodata];
return cell;
}
-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:
(NSInteger)section
{
return [[[self.fecthedResultsController sections]objectAtIndex:section]name];
NSLog(@"cellForRowAtIndexPath");
}
日志给我的错误是:
2013-04-30 12:31:04.326 passwordCore[33586:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+entityForName: nil is not a legal NSManagedObjectContext parameter searching for entity name 'Password''
*** First throw call stack: (0x1fac012 0x13e9e7e 0x10eff57 0x2d18 0x3832 0x20383e 0x204554 0xba793 0xc9937 0xc92dc 0xccdd6 0xd1a7e 0x6e2dd 0x13fd6b0 0x25a8fc0 0x259d33c 0x259d150 0x251b0bc 0x251c227 0x25beb50 0x1c39f 0x1ce81 0x2dcb5 0x2ebeb 0x20698 0x1f07df9 0x1f07ad0 0x1f21bf5 0x1f21962 0x1f52bb6 0x1f51f44 0x1f51e1b 0x1c17a 0x1dffc 0x1d3d 0x1c65) libc++abi.dylib: terminate called throwing an exception (lldb)
它指向我的代码的第 6 行。我不明白问题是 manageObjectContext 还是 entityForName:@"Password"
有人告诉我这是一种处理表格视图和数据的简单方法。现在我很沮丧...我很乐意寻求帮助。