2011年9月9日 星期五

Lala's Program Note 實作記錄: 12. 為文章列表實作 NSFetchedResultsControllerDelegate 的 methods.

since: 2011/09/09
update: 2011/09/09


在 ArticleListViewController.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;
             */
    }
}

沒有留言:

張貼留言

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