// // BeanCollectionListViewController.m // iBean // // Created by Eddie Ehlin on 2013-01-03. // Copyright (c) 2013 Eddie Ehlin. All rights reserved. // #import "BeanCollectionListViewController.h" #import "BeanCollection.h" #import "AppDelegate+Storage.h" @interface BeanCollectionListViewController () @end @implementation BeanCollectionListViewController - (id)initWithStyle:(UITableViewStyle)style { self = [super initWithStyle:style]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; NSLog(@"BeanCollectionListViewController view's loaded!"); // Uncomment the following line to preserve selection between presentations. // self.clearsSelectionOnViewWillAppear = NO; // Uncomment the following line to display an Edit button in the navigation bar for this view controller. // self.navigationItem.rightBarButtonItem = self.editButtonItem; } - (void) viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self initViewController]; //TODO: Rollback if view is "backed" to from add-state, so that nothing gets saved...that shouldn't be saved. } - (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([segue.identifier isEqualToString:@"AddNewStep1Segue"]) { NSLog(@"Going to Add new - Step 1"); //TODO: Create new bean collection object (managed context object) and push it through the segue //to the next view controller! //segue.destinationViewController Points to next view controller! Fetch it and add/set the bean collection object to use. } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } #pragma mark - Table view data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { // Return the number of sections. return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // Return the number of rows in the section (we only have one section). return self.beanCollections.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"BeanCollectionCell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; //Configure the cell and set its title BeanCollection *currentCollection = [self.beanCollections objectAtIndex:indexPath.row]; cell.textLabel.text = currentCollection.name; return cell; } // Override to support conditional editing of the table view. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { // Return NO if you do not want the specified item to be editable (we want to be able to REMOVE though). return YES; } // Override to support editing the table view. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) { // Delete the row from the data source if (self.beanCollections != nil) { if ([(AppDelegate*) [[UIApplication sharedApplication] delegate] deleteObject:[self.beanCollections objectAtIndex:indexPath.row]] != nil) { NSLog(@"deleteObject returned an error when deleting bean collection!"); #warning TODO: Handle deletion error!? } else { //Reload our bean collections array from storage (since it is no longer consistent) self.beanCollections = [(AppDelegate*) [[UIApplication sharedApplication] delegate] getBeanCollections]; } } [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; } else if (editingStyle == UITableViewCellEditingStyleInsert) { // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view } } /* // Override to support rearranging the table view. - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath { } */ /* // Override to support conditional rearranging of the table view. - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath { // Return NO if you do not want the item to be re-orderable. return YES; } */ #pragma mark - Table view delegate - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { // Navigation logic may go here. Create and push another view controller. /* <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil]; // ... // Pass the selected object to the new view controller. [self.navigationController pushViewController:detailViewController animated:YES]; */ } /***************************************************** Utility methods *****************************************************/ - (void) initViewController { NSLog(@"BeanCollectionListViewController - initViewController"); //Load bean collections from core data storage self.beanCollections = [(AppDelegate*) [[UIApplication sharedApplication] delegate] getBeanCollections]; if (self.beanCollections != nil) { [self.tableView reloadData]; } } /***************************************************** UI Actions *****************************************************/ #pragma mark - IBActions @end