2011年8月30日 星期二

Lala's Program Note 實作記錄: 6. NSFetchedResultsController 之三

since: 2011/08/30
update: 2011/12/01


將 Fetched Results Controller 合併到 Table 中.


A. 修改 NoteBookViewController.m 如下:
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return [[self.fetchedResultsController sections] count]; // it always return 1;
}

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

    return [sectionInfo numberOfObjects];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"NoteBookCell";
   
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
   
    // Configure the cell...
    // * need to implement the configureCell:atIndexPath: method
    [self configureCell:cell atIndexPath:indexPath];
   
    return cell;
}

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

B. 在 NoteBookViewController.m 裡, 實作 configureCell:atIndexPath: method
      1. 在 @implementation 字串的前一行宣告一個區塊式的 private method:
@interface NoteBookViewController() // 小括號
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath;
@end

@implementation NoteBookViewController

      2. 接著在實作區, 實作此 method:
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
    NSManagedObject *noteBook = [self.fetchedResultsController objectAtIndexPath:indexPath];

    cell.textLabel.text = [noteBook valueForKey:@"name"];
    cell.textLabel.textColor = [UIColor brownColor];

}

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

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


     2. 在 NoteBookViewController.m 新增以下的 method:

- (void)saveContext {
    NSManagedObjectContext *context = [fetchedResultsController managedObjectContext];

    NSError *error = nil;
    if (![context save:&error]) {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }
}

- (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 給
        noteBookViewController :

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

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

}

沒有留言:

張貼留言

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