[[self.fetchedResultsController sections] count];核心数据uitableview部分计数结果为空。为什么?

时间:2013-05-01 08:03:09

标签: ios uitableview core-data sections

我的实体名称是Password,它有4个字符串属性,其中一个是“type”。 我希望表有尽可能多的部分类型.. 我究竟做错了什么?! 我没有问题,但桌子只是空的,而且部分计数结果为'0'。

我的代码:

-(NSFetchedResultsController*)fecthedResultsController
{

    AppDelegate *delegate = [UIApplication sharedApplication].delegate;
    NSManagedObjectContext *mainViewcontrollerLocalContext = delegate.managedObjectContext;

    if(fecthedResultsController!=nil)
    {
        NSLog(@"fecthedResultsController1");
        return fecthedResultsController;
    }
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Password" inManagedObjectContext:mainViewcontrollerLocalContext];
    [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:mainViewcontrollerLocalContext sectionNameKeyPath:@"type" cacheName:nil];
    fecthedResultsController.delegate=self;
    NSLog(@"fecthedResultsController2");
    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.
    NSLog(@"numberOfSectionsInTableView %i",[[self.fecthedResultsController sections]count]);
    return [[self.fecthedResultsController sections]count];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    id<NSFetchedResultsSectionInfo>secInfo=[[self.fecthedResultsController sections]objectAtIndex:section];
    NSLog(@"numberOfRowsInSection");
    return [secInfo numberOfObjects];

}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    // Configure the cell...
    Password*pass=[self.fecthedResultsController objectAtIndexPath:indexPath];
    cell.textLabel.text=pass.userName;
//    cell.detailTextLabel.text=pass.Password;
    NSLog(@"cellForRowAtIndexPath");

    return cell;
}

-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:
(NSInteger)section
{
    return [[[self.fecthedResultsController sections]objectAtIndex:section]name];
    NSLog(@"cellForRowAtIndexPath");
}

日志:

2013-05-01 10:43:59.038 passwordCore[36050:c07] fecthedResultsController2
2013-05-01 10:43:59.038 passwordCore[36050:c07] numberOfSectionsInTableView 0
2013-05-01 10:43:59.039 passwordCore[36050:c07] fecthedResultsController1

写入数据库工作得很好!我有一个记录所有数据库的功能,它很好。

我做错了什么?!?!?!?!?!

1 个答案:

答案 0 :(得分:0)

您似乎忘记在获取的结果控制器上调用performFetch:

-(NSFetchedResultsController*)fecthedResultsController
{
    // ...

    fecthedResultsController=[[NSFetchedResultsController alloc]initWithFetchRequest:fetchRequest managedObjectContext:mainViewcontrollerLocalContext sectionNameKeyPath:@"type" cacheName:nil];
    fecthedResultsController.delegate=self;

    // Insert this code:
    NSError *error;
    if (![fecthedResultsController performFetch:&error]) {
        NSLog("Error: %@", [error localizedDescription]);
    }

    NSLog(@"fecthedResultsController2");
    return fecthedResultsController;
}