2011年8月30日 星期二

Lala's Program Note 實作記錄: 5. NSFetchedResultsController 之二

since: 2011/08/30
update: 2011/08/30


A. 說明:
   1. 之前由於已將 NoteBookViewController 設為 fetchedResultsController 成員變數
      的 delegate, 所以必須實作 NSFetchedResultsControllerDelegate 這個 protocol
      的 methods.

B. 實作 NSFetchedResultsControllerDelegate protocol 的 methods:
   在 NoteBookViewController.m 實作以下的 methods.

#pragma mark -
#pragma mark Fetched results controller delegate
// * tells this controller's table view to start a series of updates
// * it can initiate a sequence of changes through its beginUpdates: method.
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
    [self.tableView beginUpdates];
}

// * is called when the changes are done.
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
    [self.tableView endUpdates];
}

// only for supports sections (目前沒用到)
// * is called when a section changes(added or deleted)
/*
- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id<NSFetchedResultsSectionInfo>)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type {

    switch (type) {
        case NSFetchedResultsChangeInsert:
            [self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
            break;
           
        case NSFetchedResultsChangeDelete:
            [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
            break;
    }
}
*/

// * is called when a managed object changes(added, deleted, edited, or moved)
// * 需要再配合之後實作 configureCell:atIndexPath: method
// * configureCell:atIndexPath: method will be responsible for configuring a single table cell, 
//   and you will reuse this method in the tableView:cellForRowAtIndexPath: method.
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {
   
    UITableView *tableView = self.tableView;
    switch (type) {
        // An object has been added (inserted)
        case NSFetchedResultsChangeInsert:
            [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;
           
        // An object has been deleted
        case NSFetchedResultsChangeDelete:
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;
           
        // An object has been updated (edited)
        case NSFetchedResultsChangeUpdate:
            // need to implement the configureCell:atIndexPath: method
            [self configureCell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
            break;
           
        // An object has been moved (not used here)
        /*   
        case NSFetchedResultsChangeMove:
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

            [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;
        */
    }
}

沒有留言:

張貼留言

注意:只有此網誌的成員可以留言。