Adding the CustomerListController

Goal: Create the customer list view.

  1. In the SUP101 Xcode project, select File > New > New File.
  2. Select UIViewController subclass and click Next.
  3. Select UITableViewController in the Subclass of field, select With XIB for user interface, and then click Next.
  4. Enter CustomerListController in the Save as field and click Save.
    The files CustomerListController.h, CustomerListController.m, and CustomerListController.xib are created in the Project Navigator.
  5. Click the CustomerListController.m file, and replace the existing code with the provided code.
    #import "CustomerListController.h"
    #import "SUP101AppDelegate.h"
    #import "DetailController.h"
    
    #import "SUP101_Customer.h"
    
    
    @implementation CustomerListController
    @synthesize customerList, childController;
    
    - (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 {
        self.customerList = nil;
        self.childController = nil;
        [super dealloc];
    }
    
    
    @end
    
  6. Click the CustomerListController.h file, and replace the existing code with the provided code.
    #import <UIKit/UIKit.h>
    @class DetailController;
    
    @interface CustomerListController : UITableViewController <UITableViewDelegate, UITableViewDataSource> {          
    }
    
    @property (nonatomic, retain) NSArray *customerList;
    @property (nonatomic, retain) DetailController *childController;
    
    @end
    
Related tasks
Adding the SubscribeController View Controller
Adding the DetailController