1 / 12

Organizing data

Organizing data. So far, we have hard coded the table data We should have a model for that For example 2 parallel arrays: A 1-dim array of strings, the section headers A 2-dim array of strings, the row headers. Organizing data.

Download Presentation

Organizing data

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Organizing data • So far, we have hard coded the table data • We should have a model for that • For example 2 parallel arrays: • A 1-dim array of strings, the section headers • A 2-dim array of strings, the row headers

  2. Organizing data • It is much easier to code all the previous methods setting up the table view with such data structures

  3. 2-dim array for the table data • You can use _objects (already coded) or use your own array instance variable • If array name is toc (i.e. table of contents), a 2-dim array of strings • # of sections = [toc count] • # of rows per section = [[toc objectAtIndex: section] count] • Need parallel single-dim array for section titles

  4. Organizing data • For method setting text for all cells, use • [[toc objectAtIndex: indexPath.section] objectAtIndex: indexPath.row] • Be careful about the didSelect.. method • NSDate *object = _objects[indexPath.row]; •  may CRASH your app if you used _objects and did not organize _objects as the template expects

  5. Universal App • We may also want to have a different View Controller for the iPhone version (different dimensions) • We can keep the DetailViewController for the iPad version

  6. Universal App • Add a New File, a subclass of UIViewController, name it IPhoneDetailViewController, with or without a XIB file • In the .h file, add instance variables section, row, subChapter, and the method setSection:Row:SubChapter: • Implement the method in the .m file

  7. Universal App • In MasterViewController’s tableView:didSelectRowAtIndexPath: method, add an else block (iPhone scenario) • In it, instantiate an IPhoneDetailViewController, push it, call setSection:Row:SubChapter: • We also need to put something in the detail View, say a label with section, row and subChapter: we code an update method for that

  8. UINavigationController • A UIViewController (the superclass of UITableViewController) has a UINavigationController property; it manages the presentation of hierarchical views • It is the navigation controller that pushes the detail view controller

  9. In else block • // Create a IPhoneDetailViewController • IPhoneDetailViewController *ipdvc = [[IPhoneDetailViewController alloc] init]; • // Push it onto stack • [self.navigationController pushViewController: ipdvc animated:YES];

  10. In else block • // call setSection:…. • [ipdvc setSection: indexPath.section Row:indexPath:row SubChapter: [[toc objectAtIndex: indexPath.section] objectAtIndex: indexPath.row]; • // update detail View • [ipdvc update];

  11. Update method • // Create a label • CGRect frame = … • UILabel *label = … • // set label’s text and add it to the View • label.text = [NString stringWithFormat: @”section %d, row %d, %@”, section, row, subChapter]; • [self.view addSubview: label];

  12. Undo the magic • When we run, we are still going to the existing View of the template (via the segue from the table View) • In Main_iPhone.storyboard, delete the segue between the master View and the detail View (no longer used)

More Related