Design with iPhone Project 6: Table view, app preferences, sandbox, file storage

Warning:
Content of this website may change at any time up until class meeting.

Project Topics


Preparation


Create a new iOS app in Xcode

p6fig-navBased.jpg
Fig. 1
p6fig_newProject.jpg
Fig. 2 Xcode Workspace Window



Table Views
p6fig-tableCtrlr.jpg
Fig. 3
p6fig-unpop.jpg
Fig. 4
    NSMutableArray *myTableData1;
    NSMutableArray *myTableData2;
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title=@"tpw Table";
    myTableData1 = [[NSMutableArray alloc] initWithObjects:@"one",@"two",@"three",nil];
    myTableData2 = [[NSMutableArray alloc] initWithObjects:@"four",@"five",nil];
}
- (void)dealloc
{
    [myTableData1 release];
    [myTableData2 release];
    [super dealloc];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    switch (section) {
        case 0:
            return [myTableData1 count];
            break;
       case 1:
            return [myTableData2 count];
           break;
        default:
            return 0;
            break;
   }
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    switch (section) {
        case 0:
            return @"Section 1";
            break;
        case 1:
            return @"Section 2";
            break;
       default:
            return @"title error";
           break;
    }
}
- (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];
    }
    // Configure the cell.
    switch (indexPath.section) {
        case 0:
            [[cell textLabel]
             setText:[myTableData1 objectAtIndex:indexPath.row]];
            break;
        case 1:
            [[cell textLabel]
             setText:[myTableData2 objectAtIndex:indexPath.row]];
            break;
        default:
            [[cell textLabel]
             setText:@"cell error"];
            break;
   }
    return cell;
}
p6fig-groupedTableAttr.jpg
Fig. 5
p6fig-appSim1.jpg
Fig. 6
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIAlertView *myAlert;
    NSString    *myAlertText;
    switch (indexPath.section) {
        case 0:
            myAlertText=[[NSString alloc]
                           initWithFormat:
                           @"You chose section %i row %i named %@",
                           indexPath.section, indexPath.row,
                           [myTableData1 objectAtIndex: indexPath.row]];
            break;
        case 1:
            myAlertText=[[NSString alloc]
                           initWithFormat:
                           @"You chose section %i row %i named %@",
                           indexPath.section, indexPath.row,
                           [myTableData2 objectAtIndex: indexPath.row]];
            break;
        default:
            myAlertText=[[NSString alloc]
                           initWithFormat:
                           @"Unknown choice?"];
            break;
    }
   
    myAlert = [[UIAlertView alloc]
                     initWithTitle: @"Item Selected"
                     message: myAlertText
                     delegate: nil
                     cancelButtonTitle: @"Cancel"
                     otherButtonTitles: nil];
    [myAlert show];
    [myAlert release];
    [myAlertText release];   
}


p6fig-appSim2.jpg
Fig. 7


Navigation Application 
p6fig-hierarchy.jpg
Fig. 8
p6fig-copyPics.jpg
Fig. 9
- (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];
    }

    // Configure the cell.
    switch (indexPath.section) {
        case 0:
            [[cell textLabel]
             setText:[myTableData1 objectAtIndex:indexPath.row]];
            break;
        case 1:
            [[cell textLabel] setText:[[myTableData2  objectAtIndex: indexPath.row] objectForKey:@"name"]];
            [[cell imageView] setImage:[UIImage imageNamed:[[myTableData2  objectAtIndex: indexPath.row] objectForKey:@"picture"]]];
            cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
            break;
        default:
            [[cell textLabel]
             setText:@"cell error"];
            break;
   }
   
    return cell;
}
-- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title=@"tpw Table";
    myTableData1 = [[NSMutableArray alloc] initWithObjects:@"one",@"two",@"three",nil]; 
     myTableData2=[[NSMutableArray alloc] init];
    [myTableData2 addObject:[[NSMutableDictionary alloc]
                           initWithObjectsAndKeys:@"UNCC Sports",@"name",
                           @"p6fig-49rs-icon.png",@"picture",
                           @"http://www.charlotte49ers.com/",@"url",nil]];
    [myTableData2 addObject:[[NSMutableDictionary alloc]
                           initWithObjectsAndKeys:@"49er Football",@"name",
                           @"p6fig-49r-football-icon.png",@"picture",
                           @"http://www.charlotte49erfootball.com/",@"url",nil]];
}
p6fig-49r-football-iconddViewCtrl.jpg
Fig. 10


p6fig-adddWebView.jpg
Fig. 11
p6fig-itemOutlets.jpg
Fig. 12
@interface itemDetailViewController : UIViewController { 
    UIWebView *myItemWebView;
    UILabel *myItemLabel;
    NSString *myWebsite;
}
@property (nonatomic, retain) IBOutlet UIWebView *myItemWebView;
@property (nonatomic, retain) IBOutlet UILabel *myItemLabel;
@property (nonatomic, retain) NSString *myWebsite;
@end

- (void)viewDidLoad
{
    // Do any additional setup after loading the view from its nib.
    NSURL * baseURL;
    baseURL=[[NSURL alloc] initWithString:self.myWebsite];
    self.myItemLabel.text=self.myWebsite;
    [myItemWebView loadRequest:[NSURLRequest requestWithURL:baseURL]];
    [super viewDidLoad];
}
- (void)dealloc
{

    [myItemWebView release];
    [myItemLabel release];
    [super dealloc];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIAlertView *myAlert;
    NSString    *myAlertText;
    itemDetailViewController *myItemDetailView; 

    switch (indexPath.section) {
        case 0:
            myAlertText=[[NSString alloc]
                           initWithFormat:
                           @"You chose section %i row %i named %@",
                           indexPath.section, indexPath.row,
                           [myTableData1 objectAtIndex: indexPath.row]];
            myAlert = [[UIAlertView alloc]
                       initWithTitle: @"Item Selected"
                       message: myAlertText
                       delegate: nil
                       cancelButtonTitle: @"Cancel"
                       otherButtonTitles: nil];
            [myAlert show];
            [myAlert release];
            [myAlertText release];   
            break;
        case 1:
            myItemDetailView=[[itemDetailViewController alloc] initWithNibName:
             @"itemDetailViewController" bundle:nil];
            myItemDetailView.myWebsite=[[myTableData2 objectAtIndex: indexPath.row] objectForKey:@"url"];
            myItemDetailView.title= [[myTableData2 objectAtIndex: indexPath.row] objectForKey:@"name"];
            [self.navigationController pushViewController:
            myItemDetailView animated:YES];
            [myItemDetailView release];   
            break;
        default:
            myAlertText=[[NSString alloc]
                           initWithFormat:
                           @"Unknown choice?"];
            break;
    }
}
p6fig-appSim3.jpg
Fig. 13
p6fig-appSim4.jpg
Fig. 14


Saving Application Data & Preferences
p6fig-appSim5.jpg         p6fig-appSim6.jpg
Fig. 15                                       Fig.16
- (void)viewDidLoad
{
    NSUserDefaults *userDefaults=[NSUserDefaults standardUserDefaults];
    if([userDefaults floatForKey:@"sliderVal"])
    {
        myDetailSlider.value =[userDefaults floatForKey:@"sliderVal"];
        mySliderLabel.text=
        [[NSString alloc] initWithFormat:@"slide=%1.2f",
         myDetailSlider.value];
    }
    [super viewDidLoad];
}
- (IBAction)mySliderAction:(id)sender {
    mySliderLabel.text=
    [[NSString alloc] initWithFormat:@"slide=%1.2f",
     myDetailSlider.value];
    NSUserDefaults *userDefaults=[NSUserDefaults standardUserDefaults];
    [userDefaults setFloat:myDetailSlider.value forKey:@"sliderVal"];
}




Demonstration



Report



Copyright 2011 by T.P. Weldon


Apple, iPhone, iPad, and Xcode  are registered trademarks of Apple Inc.