0

数日間、データベース情報 (コアデータ) を fecthedResultsController を使用してテーブルビューに転送しようとしましたが、うまくいきません!

私のエンティティ名は「パスワード」で、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" なのかわかりません

これは、テーブルビューとデータを処理するための簡単な方法だと言われました..今、私はただイライラしています...私はいくつかの助けが大好きです.

4

1 に答える 1