Note_Master-Detail Application(iOS template)_05_ YJYMasterViewController.m

 

//  YJYMasterViewController.m

 

#import "YJYMasterViewController.h"

 

#import "YJYDetailViewController.h"

 

@interfaceYJYMasterViewController ()

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath;

@end

 

@implementation YJYMasterViewController

 

- (void)awakeFromNib

{

    NSLog(@"awakeFromNib() begin...");

    

    self.clearsSelectionOnViewWillAppear = NO;

    self.contentSizeForViewInPopover = CGSizeMake(320.0, 600.0);

    [superawakeFromNib];

    

    NSLog(@"awakeFromNib() over...");

}

 

- (void)viewDidLoad

{//释放视图资源

    NSLog(@"viewDidLoad() begin...");

 

    [superviewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

    self.navigationItem.leftBarButtonItem = self.editButtonItem;

 

    UIBarButtonItem *addButton = [[UIBarButtonItemalloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAddtarget:selfaction:@selector(insertNewObject:)];

    self.navigationItem.rightBarButtonItem = addButton;

    self.detailViewController = (YJYDetailViewController *)[[self.splitViewController.viewControllerslastObject] topViewController];

 

    NSLog(@"viewDidLoad() over...");

}

 

- (void)didReceiveMemoryWarning

{

    NSLog(@"didReceiveMemoryWarning() begin...over");

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

- (void)insertNewObject:(id)sender

{

    NSLog(@"insertNewObject() begin...");

    NSManagedObjectContext *context = [self.fetchedResultsControllermanagedObjectContext];

    NSEntityDescription *entity = [[self.fetchedResultsControllerfetchRequest] entity];

    NSManagedObject *newManagedObject = [NSEntityDescriptioninsertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];

    

    // If appropriate, configure the new managed object.

    // Normally you should use accessor methods, but using KVC here avoids the need to add a custom class to the template.

    [newManagedObject setValue:[NSDate date] forKey:@"timeStamp"];

    

    // Save the context.

    NSError *error = nil;

    if (![context save:&error]) {

         // Replace this implementation with code to handle the error appropriately.

         // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. 

        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);

        abort();

    }

    

    NSLog(@"insertNewObject() over...");

}

 

#pragma mark - Table View

 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    NSLog(@"numberOfSectionsInTableView being...over");

    return [[self.fetchedResultsControllersections] count];

}

 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    NSLog(@"tableView numberOfRowsInSection() beging...");

    id <NSFetchedResultsSectionInfo> sectionInfo = [self.fetchedResultsController sections][section];

    NSLog(@"tableView numberOfRowsInSection() over...");

    return [sectionInfo numberOfObjects];

}

 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    NSLog(@"tableView cellForRowAtIndexPath() beging...");

 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"forIndexPath:indexPath];

    [self configureCell:cell atIndexPath:indexPath];

    NSLog(@"tableView cellForRowAtIndexPath() over...");

 

    return cell;

}

 

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath

{//该方法控制master区的可编辑(删除)操作

    // Return NO if you do not want the specified item to be editable.

    return YES;

}

 

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

{

    if (editingStyle == UITableViewCellEditingStyleDelete) {

        NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];

        [context deleteObject:[self.fetchedResultsController objectAtIndexPath:indexPath]];

        

        NSError *error = nil;

        if (![context save:&error]) {

             // Replace this implementation with code to handle the error appropriately.

             // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. 

            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);

            abort();

        }

    }   

}

 

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath

{

    // The table view should not be re-orderable.

    returnYES;

}

 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

    NSManagedObject *object = [[self fetchedResultsController] objectAtIndexPath:indexPath];

    self.detailViewController.detailItem = object;

}

 

#pragma mark - Fetched results controller

 

- (NSFetchedResultsController *)fetchedResultsController

{

    if (_fetchedResultsController != nil) {

        return_fetchedResultsController;

    }

    

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

    // Edit the entity name as appropriate.

    NSEntityDescription *entity = [NSEntityDescriptionentityForName:@"Event"inManagedObjectContext:self.managedObjectContext];

    [fetchRequest setEntity:entity];

    

    // Set the batch size to a suitable number.

    [fetchRequest setFetchBatchSize:20];

    

    // Edit the sort key as appropriate.

    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"timeStamp" ascending:NO];

    NSArray *sortDescriptors = @[sortDescriptor];

    

    [fetchRequest setSortDescriptors:sortDescriptors];

    

    // Edit the section name key path and cache name if appropriate.

    // nil for section name key path means "no sections".

    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsControlleralloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContextsectionNameKeyPath:nilcacheName:@"Master"];

    aFetchedResultsController.delegate = self;

    self.fetchedResultsController = aFetchedResultsController;

    

NSError *error = nil;

if (![self.fetchedResultsControllerperformFetch:&error]) {

    // Replace this implementation with code to handle the error appropriately.

    // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. 

    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);

    abort();

}

    

    return_fetchedResultsController;

}    

 

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller

{

    [self.tableViewbeginUpdates];

}

 

- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo

           atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type

{

    switch(type) {

        caseNSFetchedResultsChangeInsert:

            [self.tableViewinsertSections:[NSIndexSetindexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];

            break;

            

        caseNSFetchedResultsChangeDelete:

            [self.tableViewdeleteSections:[NSIndexSetindexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];

            break;

    }

}

 

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject

       atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type

      newIndexPath:(NSIndexPath *)newIndexPath

{

    UITableView *tableView = self.tableView;

    

    switch(type) {

        caseNSFetchedResultsChangeInsert:

            [tableView insertRowsAtIndexPaths:@[newIndexPath]withRowAnimation:UITableViewRowAnimationFade];

            break;

            

        caseNSFetchedResultsChangeDelete:

            [tableView deleteRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationFade];

            break;

            

        caseNSFetchedResultsChangeUpdate:

            [self configureCell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];

            break;

            

        caseNSFetchedResultsChangeMove:

            [tableView deleteRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationFade];

            [tableView insertRowsAtIndexPaths:@[newIndexPath]withRowAnimation:UITableViewRowAnimationFade];

            break;

    }

}

 

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller

{

    [self.tableViewendUpdates];

}

 

/*

// Implementing the above methods to update the table view in response to individual changes may have performance implications if a large number of changes are made simultaneously. If this proves to be an issue, you can instead just implement controllerDidChangeContent: which notifies the delegate that all section and object changes have been processed. 

 

 - (void)controllerDidChangeContent:(NSFetchedResultsController *)controller

{

    // In the simplest, most efficient, case, reload the table view.

    [self.tableView reloadData];

}

 */

 

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath

{

    NSLog(@"configureCell atIndexPath() begin...");

    

    NSManagedObject *object = [self.fetchedResultsController objectAtIndexPath:indexPath];

    cell.textLabel.text = [[object valueForKey:@"timeStamp"] description];

 

    NSLog(@"configureCell atIndexPath() over...");

}

 

@end

Note_Master-Detail Application(iOS template)_05_ YJYMasterViewController.m,,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。