NSFetchedResultsControllerDelegate& UITableViewDelegate行为

时间:2011-02-28 06:51:24

标签: ios uitableview delegates nsfetchrequest

我遇到了FRC和TableView委托的行为似乎不一致:

  • performFetch 的初始调用会导致委托方法按预期调用。但是,如果我更新FRC的 baseFetch 上的谓词,然后再次调用 performFetch ,则永远不会调用FRC委托方法,并且不会使用新数据更新TableView。为了强制表更新并显示新数据,我明确地调用 reloadData
  • performFetch 的初始调用正确构建了TableView的部分。如果我更改FRC(新FCR具有不同的 fetchRequest sectionNameKeyPath )并再次调用 performFetch ,则表和部分将更新以匹配新的结果,如预期的那样。但是,如果我然后返回到原始FRC并调用 performFetch ,则这些部分具有正确的名称,但具有先前FRC的部分/行结构。要强制更新部分,我明确地调用 reloadSectionIndexTitles

我的感觉是,如果事情发生变化,代表们应该在我们调用 performFetch 的任何时候解雇。显式调用 reloadData reloadSectionIndexTitles 似乎是一个昂贵且不必要的步骤,因为代理人的存在就是这样。我错过了什么吗?

以下是相关代码:

NSFetchedResultsControllerDelegate

    - (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
    // The fetch controller is about to start sending change notifications, so prepare the table view for updates.
    [self.myTable beginUpdates];
}


- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {

    switch(type) {

        case NSFetchedResultsChangeInsert:
            [self.myTable insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeDelete:
            [self.myTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeUpdate:
            [self configureCell:[self.myTable cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
            break;

        case NSFetchedResultsChangeMove:
            [self.myTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            // Reloading the section inserts a new row and ensures that titles are updated appropriately.
            [self.myTable reloadSections:[NSIndexSet indexSetWithIndex:newIndexPath.section] withRowAnimation:UITableViewRowAnimationFade];
            break;
    }

}


- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type {

    switch(type) {

        case NSFetchedResultsChangeInsert:
            [self.myTable insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeDelete:
            [self.myTable deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
            break;
    }
}


- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
    // The fetch controller has sent all current change notifications, so tell the table view to process all updates.
    [self.myTable endUpdates];
}

的UITableViewDelegate

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return [[currentFCR sections] count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { 
    id <NSFetchedResultsSectionInfo> sectionInfo = [[currentFCR sections] objectAtIndex:section];
    return section.name;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    id <NSFetchedResultsSectionInfo> sectionInfo = [[currentFCR sections] objectAtIndex:section];
    return [sectionInfo numberOfObjects];
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }

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

    return cell;
}

撷取

- (NSFetchedResultsController *)FRC1 {

    if (FRC1 != nil) {
        return FRC1;
    }

    NSFetchRequest *request = [[DataProvider instance] getFetch];
    [[DataProvider instance] sortListByFirstSort:request];
    [request setFetchBatchSize:20];

    FRC1 = [[NSFetchedResultsController alloc] initWithFetchRequest:request 
                                                                   managedObjectContext:[[DataProvider instance] managedObjectContext] 
                                                                     sectionNameKeyPath:@"groupName" 
                                                                              cacheName:nil];
    return FRC1;    

}

- (NSFetchedResultsController *)FRC2 {

    if (FRC2 != nil) {
        return FRC2;
    }

    NSFetchRequest *request = [[DataProvider instance] getFetch];
    [[DataProvider instance] sortListBySecondSort:request]; 


 [request setFetchBatchSize:20];

        FRC2 = [[NSFetchedResultsController alloc] initWithFetchRequest:request 
                                                       managedObjectContext:[[DataProvider instance] managedObjectContext] 
                                                         sectionNameKeyPath:@"name" 
                                                                  cacheName:nil];
        return FRC2;    

    }

    -(void)doFetch{
    NSError *error;
    if (![currentFCR performFetch:&error]) {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        exit(-1);  // Fail
    }
    [self.myTable reloadData];
    [self.myTable reloadSectionIndexTitles];
}

1 个答案:

答案 0 :(得分:0)

NSFetchedResultsControllerDelegate方法controllerWillChangeContent:等只有在NSFetchedResultsController实例上设置委托时才会被调用。我没有在您的代码中看到任何代理人。

初始表加载(或显式调用reloadTable)将导致调用UITableViewDataSource方法,从currentFCR获取数据。如果未在NSFetchedResultsController上设置委托,则不会调用NSFetchedResultsControllerDelegate方法。

此外,只有在NSManagedObjectContext中更改(或添加/删除)任何相关托管对象时,才会调用NSFetchedResultsControllerDelegate方法。

如果要替换NSFetchedResultsController对象,则需要显式重新加载表。