2011年9月9日 星期五

Lala's Program Note 實作記錄: 13. 將文章列表與 Fetched Results Controller 合併

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


A. 修改 ArticleListViewController.m 如下:

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

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

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

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //static NSString *CellIdentifier = @"Cell";
    static NSString *CellIdentifier = @"ArticleListCell";
   
    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. 在 ArticleListViewController.m 裡, 實作 configureCell:atIndexPath: method
   1. 在 @implementation 字串的前一行宣告一個區塊式的 private method:
@interface ArticleListViewController()
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath;
@end

@implementation ArticleListViewController

   2. 接著在實作區, 實作此 method:
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {

    NSManagedObject *articleList = [self.fetchedResultsController objectAtIndexPath:indexPath];
   
    cell.textLabel.text = [articleList valueForKey:@"title"];
    cell.textLabel.textColor = [UIColor brownColor];

}


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

   2. 在 ArticleListViewController.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. 在 NoteBookViewController.m 中提供 managed object contexts 給 articleListViewController :
-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {

    ArticleListViewController *articleListViewController = [[ArticleListViewController alloc] initWithStyle:UITableViewStylePlain];

    // set controller's managed object context member right after initializing
    articleListViewController.managedObjectContext = self.managedObjectContext;
   
    NSManagedObject *noteBook = [[self fetchedResultsController] objectAtIndexPath:indexPath];
    articleListViewController.title = [NSString stringWithFormat:@"%@",[noteBook valueForKey:@"name"]];
   
    //@add for relationship used
    articleListViewController.noteBook = noteBook;

    // not need to Wrap it in an instance of UINavigationController, just do:
    [self.navigationController pushViewController:articleListViewController animated:YES];

    [articleListViewController release];
}

沒有留言:

張貼留言

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