2011年9月21日 星期三

Lala's Program Note 實作記錄: 23. 瀏覽我的最愛之二

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

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


B. 實作 NSFetchedResultsControllerDelegate protocol 的 methods:
    在 FavoriteViewController.m 實作以下的 methods.
    說明: 在此, "瀏覽我的最愛", 主要以瀏覽為主, 資料異動的功能僅供參考.

//@add
#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;
             */
    }
}

沒有留言:

張貼留言

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