Wrox Programmer Forums
|
BOOK: Professional iPhone and iPad Database Application Programming
This is the forum to discuss the Wrox book Professional iPhone and iPad Database Application Programming by Patrick Alessi; ISBN: 978-0-470-63617-6
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional iPhone and iPad Database Application Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 23rd, 2011, 01:23 PM
EEE EEE is offline
Registered User
 
Join Date: May 2011
Posts: 6
Thanks: 2
Thanked 0 Times in 0 Posts
Default Chapter 7 ViewTaskController - 3 Sections in Table

In the ViewTaskController, as depicted on pg. 172 in figure 7-5, I'd like to split up the table into 3 sections and show the 6 rows of data split up over 3 table sections with section titles like the following:

Section 1
Text
Priority

Section 2
Due Date
Location

Section 3
Hi-Pri Task
Tasks due sooner than this one

Can anyone please explain exactly how this would be done in order to define the three section titles, section the table into 3 sections, define exactly how many rows per section, and push the correct data to each row in each section?

I know where the numberOfSectionsInTableView and numberOfRowsInSection code is in ViewTaskController.m, but I don't understand how to define the exact sections and data for each section in order to achieve the 3 sections as mentioned above.

Thanks in advance for your help.
 
Old May 23rd, 2011, 10:26 PM
Friend of Wrox
 
Join Date: Aug 2010
Posts: 298
Thanks: 1
Thanked 62 Times in 60 Posts
Default

The following changes to ViewTaskController.m produce the result you appear to be looking for. (Changes and additions in bold)

Code:
#pragma mark Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView 
    numberOfRowsInSection:(NSInteger)section {
    return 2;
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [NSString stringWithFormat:@"Section %d",section + 1];
}


// 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:UITableViewCellStyleValue2 
                 reuseIdentifier:CellIdentifier] autorelease];
    }
    
    // Set up the cell...
    int choice = indexPath.section * 2 + indexPath.row;
    switch (choice) {
        case 0:
            cell.textLabel.text = @"Text";
            cell.detailTextLabel.text = managedTaskObject.text;
            break;
        case 1:
            cell.textLabel.text = @"Priority";
            
            // Get the priority number and convert it to a string
            NSString* priorityString=nil;
            
            switch ([managedTaskObject.priority intValue]) {
                case 0:
                    priorityString = @"None";
                    break;
                case 1:
                    priorityString = @"Low";
                    break;
                case 2:
                    priorityString = @"Medium";
                    break;
                case 3:
                    priorityString = @"High";
                    break;
                default:
                    break;
            }
            
            cell.detailTextLabel.text = priorityString;
            [priorityString release];
            
            break;
        case 2:
            cell.textLabel.text = @"Due Date";
            
            //  Create a date formatter to format the date from the picker
            NSDateFormatter* df = [[NSDateFormatter alloc] init];
            [df setDateStyle:NSDateFormatterLongStyle];
            cell.detailTextLabel.text = 
                [df stringFromDate:managedTaskObject.dueDate ];
            [df release];
            
            break;
        case 3:
            cell.textLabel.text = @"Location";
            Location* locationObject = managedTaskObject.location;
            if (locationObject!=nil)
            {
                cell.detailTextLabel.text = locationObject.name;
            }
            else {
                cell.detailTextLabel.text = @"Not Set"; 
                
            }
            
            break;   
        case 4:
            // Show hi-pri tasks alert
            cell.detailTextLabel.text = @"Hi-Pri Tasks";
            break;
        case 5:
            // Show sooner tasks alert
            cell.detailTextLabel.text = @"Tasks due sooner than this one";
            break;
            
        default:
            break;
    }
    
    return cell;
}
Bob
The Following User Says Thank You to thepianoguy For This Useful Post:
EEE (May 23rd, 2011)
 
Old May 23rd, 2011, 11:00 PM
EEE EEE is offline
Registered User
 
Join Date: May 2011
Posts: 6
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Thank you very much for your reply and for the code instructions. It worked perfectly! However, I may have made my example too symmetrical in section sizes and names. Can you elaborate with the following section examples. I'm having a hard time figuring out how to get specific with the Section titles and changing up the number of rows and order of rows rather than an even 2 rows per section as I had mistakenly mentioned earlier. Thanks in advance for your help. Much appreciated.

Alpha
Text

Bravo
Due Date
Location
Priority

Tango
Hi-Pri Task
Tasks due sooner than this one
 
Old May 24th, 2011, 12:54 AM
Friend of Wrox
 
Join Date: Aug 2010
Posts: 298
Thanks: 1
Thanked 62 Times in 60 Posts
Default

The following sets up the tableView as you described. Note that the reordering of the items also required reworking or the -didSelectRowAtIndexPath... method as well for the proper controller to be called. If there is anything that is not clear let me know.

Code:
#pragma mark Table view methods -- Edited to resolve forum question

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView 
 numberOfRowsInSection:(NSInteger)section {
    NSInteger returnValue = 0;
    switch (section) {
        case 0:
            returnValue = 1;
            break;
        case 1:
            returnValue = 3;
            break;
        case 2:
            returnValue = 2;
            
        default:
            break;
    }
    return returnValue;
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NSString *sectionHeader = nil;
    switch (section) {
        case 0:
            sectionHeader = @"Alpha";
            break;
        case 1:
            sectionHeader = @"Bravo";
            break;
        case 2:
            sectionHeader = @"Tango";
            break;
        default:
            break;
    }
    
    return sectionHeader;
}


// 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:UITableViewCellStyleValue2 
                 reuseIdentifier:CellIdentifier] autorelease];
    }
    
    // Set up the cell...
    
    switch (indexPath.section) {
        case 0:
            cell.textLabel.text = @"Text";
            cell.detailTextLabel.text = managedTaskObject.text;
            break;
        case 1:
            switch (indexPath.row) {
                case 0:
                    cell.textLabel.text = @"Due Date";                    
                    //  Create a date formatter to format the date from the picker
                    NSDateFormatter* df = [[NSDateFormatter alloc] init];
                    [df setDateStyle:NSDateFormatterLongStyle];
                    cell.detailTextLabel.text = 
                    [df stringFromDate:managedTaskObject.dueDate ];
                    [df release];
                    
                    break;
                case 1:
                    cell.textLabel.text = @"Location";
                    Location* locationObject = managedTaskObject.location;
                    if (locationObject!=nil)
                    {
                        cell.detailTextLabel.text = locationObject.name;
                    }
                    else {
                        cell.detailTextLabel.text = @"Not Set"; 
                        
                    }
                    
                    break;
                case 2:
                    cell.textLabel.text = @"Priority";
                    
                    // Get the priority number and convert it to a string
                    NSString* priorityString=nil;
                    
                    switch ([managedTaskObject.priority intValue]) {
                        case 0:
                            priorityString = @"None";
                            break;
                        case 1:
                            priorityString = @"Low";
                            break;
                        case 2:
                            priorityString = @"Medium";
                            break;
                        case 3:
                            priorityString = @"High";
                            break;
                        default:
                            break;
                    }
                    
                    cell.detailTextLabel.text = priorityString;
                    [priorityString release];
                    break;
                default:
                    break;
            }
            break;
        case 2:
            switch (indexPath.row) {
                case 0:
                    // Show hi-pri tasks alert
                    cell.detailTextLabel.text = @"Hi-Pri Tasks";
                    break;
                case 1:
                    // Show sooner tasks alert
                    cell.detailTextLabel.text = @"Tasks due sooner than this one";
                    break;
                default:
                    break;
            }
            
        default:
            break;
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView 
    didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    //  Deselect the currently selected row according to the HIG
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    //  Based on the section and selected row, choose which controller to push
    switch (indexPath.section) {
        case 0:
        {
            EditTextController* etc = [[EditTextController alloc] 
                                       initWithStyle:UITableViewStyleGrouped];
            etc.managedObject = self.managedTaskObject;
            [email protected]"text";
            etc.managedObjectContext = self.managedObjectContext;
            [self.navigationController pushViewController:etc animated:YES];
            [etc release];
            break;
        }
        case 1:
            switch (indexPath.row) {
                case 0:
                {
                    EditDateController* edc = [[EditDateController alloc] init];
                    
                    edc.managedTaskObject = self.managedTaskObject;
                    edc.managedObjectContext = self.managedObjectContext;
                    
                    [self.navigationController pushViewController:edc animated:YES];
                    [edc release];
                    break;
                }
                case 1:
                {
                    EditLocationController* elc = [[EditLocationController alloc] init];
                    elc.managedObjectContext = self.managedObjectContext;
                    elc.managedTaskObject = self.managedTaskObject;
                    [self.navigationController pushViewController:elc animated:YES];
                    [elc release];
                    break;
                }
                case 2:
                {
                    EditPriorityController* epc = 
                    [[EditPriorityController alloc]
                     initWithStyle:UITableViewStyleGrouped];
                    epc.managedTaskObject = self.managedTaskObject;
                    epc.managedObjectContext = self.managedObjectContext;
                    
                    [self.navigationController pushViewController:epc animated:YES];
                    [epc release];
                    break;
                }
                
                    
                default:
                    break;
            }
            break;
        case 2:
            switch (indexPath.row) {
                case 0:
                {
                    
                    UIAlertView* alert = 
                    [[[UIAlertView alloc] initWithTitle:@"Hi-Pri Tasks" 
                                                message:nil
                                               delegate:self 
                                      cancelButtonTitle:@"OK" 
                                      otherButtonTitles:nil  ] autorelease];
                    
                    // Use Fetched property to get a list of high-pri tasks
                    NSArray* highPriTasks = managedTaskObject.highPriTasks;
                    NSMutableString* alertMessage = 
                    [[[NSMutableString alloc] init] autorelease];
                    
                    // Loop through each hi-pri task to create the string for 
                    // the message
                    for (Task * theTask in highPriTasks)                
                    {
                        [alertMessage appendString:theTask.text];
                        [alertMessage appendString:@"\n"];
                    }
                    
                    alert.message = alertMessage;
                    [alert show];
                    
                    break;    
                }
                case 1:
                {
                    
                    UIAlertView* alert =
                    [[[UIAlertView alloc] initWithTitle:@"Tasks due sooner" 
                                                message:nil
                                               delegate:self 
                                      cancelButtonTitle:@"OK" 
                                      otherButtonTitles:nil  ] autorelease];
                    NSMutableString* alertMessage =
                    [[[NSMutableString alloc] init] autorelease];
                    
                    // need to get a handle to the managedObjectModel to use the stored
                    // fetch request
                    TasksAppDelegate* appDelegate =
                    [UIApplication sharedApplication].delegate;
                    NSManagedObjectModel* model = appDelegate.managedObjectModel;
                    
                    // Get the stored fetch request
                    NSDictionary* dict = 
                    [[NSDictionary alloc] 
                     initWithObjectsAndKeys:managedTaskObject.dueDate,  
                     @"DUE_DATE",nil];
                    
                    NSFetchRequest* request =
                    [model fetchRequestFromTemplateWithName:@"tasksDueSooner"
                                      substitutionVariables:dict];
                    
                    [dict release];
                    
                    NSError* error;
                    NSArray* results = 
                    [managedObjectContext executeFetchRequest:request error:&error];
                    
                    // Loop through eachtask to create the string for the message
                    for (Task * theTask in results)                
                    {
                        [alertMessage appendString:theTask.text];
                        [alertMessage appendString:@"\n"];
                    }
                    alert.message = alertMessage;
                    [alert show];
                    
                    break;    
                }
                default:
                    break;
            }
        default:
            break;
    }
}
Bob
The Following User Says Thank You to thepianoguy For This Useful Post:
EEE (May 24th, 2011)
 
Old May 24th, 2011, 01:32 AM
EEE EEE is offline
Registered User
 
Join Date: May 2011
Posts: 6
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Bob,

That all worked perfectly and is exactly what I needed. Thanks for taking the time to re-tweak this. This really helps a lot. Thank you very much, and thanks for the good catch on the didSelectRowAtIndexPath method for the correct controller to be called. Much appreciated!

Evan





Similar Threads
Thread Thread Starter Forum Replies Last Post
Config Sections Muhammad Zeeshan BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 3 February 3rd, 2008 01:23 AM
Sections formatting s_chilkury Crystal Reports 0 December 21st, 2005 06:46 AM
Delete sections according to their ID lxken XSLT 0 May 13th, 2004 06:08 AM
How to move sections lxken XSLT 5 April 28th, 2004 06:44 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.