2011年9月21日 星期三

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

since: 2011/09/21
update: 2011/12/02


將 Fetched Results Controller 合併到 Table 中:

A. 修改 FavoriteViewController.m 如下:


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    //@update
    return [[self.fetchedResultsController sections] count]; // it always return 1;
}

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


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //static NSString *CellIdentifier = @"Cell";
    static NSString *CellIdentifier = @"NoteArticleCell";
   
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        //cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        //@update for Subtitle
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }
   
    // Configure the cell...
    //@add
    // * need to implement the configureCell:atIndexPath: method
    [self configureCell:cell atIndexPath:indexPath];
   
    return cell;
}

----------------------------------------------------------------------------------------------------------

B. 在 FavoriteViewController.m 裡, 實作 configureCell:atIndexPath: method
   1. 在 @implementation 字串的前一行宣告一個區塊式的 private method:

//@add
@interface FavoriteViewController() //小括號下:宣告 private method
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath;
@end
@implementation FavoriteViewController

2. 接著在實作區, 實作此 method:

//@add
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
    NSManagedObject *article = [self.fetchedResultsController objectAtIndexPath:indexPath];
   
    cell.textLabel.text = [article valueForKey:@"title"];
    cell.textLabel.textColor = [UIColor brownColor];

    cell.detailTextLabel.text = [article valueForKey:@"subTitle"];
    cell.detailTextLabel.textColor = [UIColor darkGrayColor];
}

----------------------------------------------------------------------------------------------------------

C. 當使用者移動刪除一列資料 (in this case, we can only delete it not used yet)
   1. 在 FavoriteViewController.h 新增以下的 method:
      //@add
      - (void)saveContext;

2. 在 FavoriteViewController.m 新增以下的 method:
//@add
- (void)saveContext {
    NSManagedObjectContext *context = [fetchedResultsController managedObjectContext];
   
    NSError *error = nil;
    if (![context save:&error]) {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }
}


//@add: may not used yet
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the managed object for the given index path
        NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
       
        [context deleteObject:[self.fetchedResultsController objectAtIndexPath:indexPath]];
       
        // Save the context     
        [self saveContext];
    }
}


----------------------------------------------------------------------------------------------------------

D. 在 Lala_s_Program_NoteAppDelegate.m 中提供 managed object contexts 給
        favoriteViewController :

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    FavoriteViewController *favoriteViewController = [[FavoriteViewController alloc] initWithStyle:UITableViewStylePlain];

    // set controller's managed object context member right after initializing
    favoriteViewController.managedObjectContext = self.managedObjectContext;
    ....

}

----------------------------------------------------------------------------------------------------------

E. 結果:

沒有留言:

張貼留言

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