Adding the CustomerListController

Goal: Create the customer list view.

  1. In the SUP101 Xcode project, select File > New File.
  2. In the new File window, select the Cocoa Touch Class group, the UIViewController subclass and the UITableViewController subclass option. Unselect With XIB for user interface, then click Next.
  3. In the next window, in File Name, enter CustomerListController.m, ensure that Also create CustomerListController.h is selected, and click Finish.
    The new source files contain stub implementations of various methods.
  4. Open the CustomerListController.m file, and add this code:
    #import "CustomerListController.h"
    #import "SUP101AppDelegate.h"
    #import "DetailController.h"
    
    #import "SUP101_Customer.h"
    
    
    @implementation CustomerListController
    @synthesize customerList;
    
    - (id)initWithStyle:(UITableViewStyle)style {
        // Override initWithStyle: if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
        if (self = [super initWithStyle:style]) {
        }
        return self;
    }
    
    
    
    - (void)viewDidLoad {
        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem;
        [super viewDidLoad];
    }
    
    - (void)viewWillAppear:(BOOL)animated {
    
        self.title = @"Customers";
        NSMutableArray *array = [[NSMutableArray alloc] init];
        
        SUP101_CustomerList *customers = [SUP101_Customer findAll];
        if ([customers length] > 0)
        {
            for (SUP101_Customer * oneRec in customers)
            {
                [array addObject:oneRec];
            }
        }
        self.customerList = array;
        [array release];
        [[self tableView] reloadData];
        [super viewWillAppear:animated];
        
    }
    
    
    /*
    - (void)viewWillAppear:(BOOL)animated {
        [super viewWillAppear:animated];
    }
    */
    /*
    - (void)viewDidAppear:(BOOL)animated {
        [super viewDidAppear:animated];
    }
    */
    /*
    - (void)viewWillDisappear:(BOOL)animated {
    	[super viewWillDisappear:animated];
    }
    */
    /*
    - (void)viewDidDisappear:(BOOL)animated {
    	[super viewDidDisappear:animated];
    }
    */
    
    /*
    // Override to allow orientations other than the default portrait orientation.
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
        // Return YES for supported orientations
        return (interfaceOrientation == UIInterfaceOrientationPortrait);
    }
    */
    
    - (void)didReceiveMemoryWarning {
    	// Releases the view if it doesn't have a superview.
        [super didReceiveMemoryWarning];
    	
    	// Release any cached data, images, etc that aren't in use.
    }
    
    - (void)viewDidUnload {
    	// Release any retained subviews of the main view.
    	// e.g. self.myOutlet = nil;
    }
    
    
    #pragma mark Table view methods
    
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
        return 1;
    }
    
    
    // Customize the number of rows in the table view.
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        return [self.customerList count];
    }
    
    
    // Customize the appearance of table view cells.
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        
        static NSString *CellIdentifier = @"Cell";
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        }
        
        // Set up the cell...
    	NSUInteger row = [indexPath row];
        SUP101_Customer *customer = [customerList objectAtIndex:row];
        cell.textLabel.text = [NSString stringWithFormat:@"%@%@%@", [customer fname], @" ", [customer lname]];
        cell.accessoryType =  UITableViewCellAccessoryDisclosureIndicator;
        return cell;
    }
    
    
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        
        [self tableView:tableView accessoryButtonTappedForRowWithIndexPath:indexPath];
        
    }
    
    
    /*
    // 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.
        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
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
        }   
        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;
    }
    */
    /*
    - (UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath
    {
    	return UITableViewCellAccessoryDisclosureIndicator;
    }
    */
    - (void)tableView:(UITableView *)tableView 
    accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
    {
    	if (childController == nil)
    		childController = [[DetailController alloc] 
    						   initWithNibName:@"DetailController" bundle:nil];
    	
    	NSUInteger row = [indexPath row];
    	
    	SUP101_Customer *selectedCustomer = [customerList objectAtIndex:row];
    	childController.title = [NSString stringWithFormat:@"%d", [selectedCustomer id]];
        childController.originalObj = selectedCustomer;
        
    	SUP101AppDelegate *delegate = [[UIApplication sharedApplication] delegate];
    	[delegate.navController pushViewController:childController animated:YES];
    	
    }
    
    
    - (void)dealloc {
        [customerList release];
        [childController release];
        [super dealloc];
    }
    
    
    @end
    
  5. Save the CustomerListController.m file.
  6. Open the CustomerListController.h file, and add this code:
    #import <UIKit/UIKit.h>
    #import "DetailController.h"
    
    @interface CustomerListController : UITableViewController
            <UITableViewDelegate, UITableViewDataSource> {
                NSArray *customerList;
                DetailController *childController;
    }
    @property (nonatomic, retain) NSArray *customerList;
    @end
    
  7. Save the CustomerListController.h file.
Related tasks
Adding the SubscribeController View Controller
Adding the DetailController