Tutorial :iPhone - How to select and show a single record in Grouped Table View Format


Im making a iPhone app which includes a list of sports teams. RootViewController lists all the teams in a table view. When I click a row I want to load up a TeamViewController, which just shows the record for a single team.

The catch is Im wanting to display that single teams details in the Grouped Table View Style.

This is causing me endless issues.

I just can't seem to work out where I draw on the objects I need from. eg Do I need to use NSFetchedResultsController.

Ive implemented core data and can see example of what to do from the AppDelegate and RootViewController but which bits to replicate and which parts I just call in in my new TeamViewController have me stumped.

To setup cellForRowAtIndexPath in TeamViewController what are the main steps I need to go through so that I can pull in the single selected record and be able to manipulate it and also how do I go about laying out my Grouped Table View for that record.

More broadly what Im not seeming to get is which bits are carried forward in each new ViewController so I can make my navigation app have many pages, with each record drilling in to give the next pages view.

If this is a bit confused just ask me more questions and I'll clarify.


It sounds like your using a Master-Detail pattern wherein you have a list of teams in the master tableview and then detail view that shows the details of each team. You just have one entity "Team."

For the master tableview, you would just use a NSFetchedResultsController to fetch and order the list of teams.

The detail view controller should have a property that holds a reference to a single instance of the Team class (or a generic managed object if you don't have a custom subclass.)

In the master view's didSelectCellAtIndexPath: method, you set the detail view's team property to the Team instance at that indexPath in the FRC before you push the detail view onto the navigation controller.

You configure the grouped table in the detail view by using a simple switch statement with one case for each row in the table which in turn corresponds with a property/value of the Team class/entity. Since the table displays the property/named-values of a class and not a variable list of many objects, the number of rows and sections is always fixed. If you want multiple groups, you can nest switch statements. The outer switch is for sections and the inner switch for rows.

@property (nonatomic,retain) Team *currentTeam;  ...  - (void) configureCell:(UITableViewCell *) cell forIndexPath:(NSIndexPath *) indexPath{      switch (indexPath.section) {          case 0: // first section              switch (indexPath.row) {                  case 0:                      cell.textLabel.text=currentTeam.name                      break;                  case 1:                      cell.textLabel.text=currentTeam.homeCity                      break;                  case 2:                      // and so on                      break;          case 1: //second section              //...              break;              }              break;          default:              break;      }  }  


There isn't enough space or time to answer your question fully in this comment field, as such an answer would need to cover Core Data, but also pushing a new view controller onto a navigation controller stack, and how to work with UITableView and a table view controller. You may want to go through Apple's Core Data "Books" tutorial, which may help you with some of this.


The following is my own notes and to hopefully help anyone who is looking to answer the question I asked in the title.

To answer the question of 'How to select and show a single record in Grouped Table View Format' more generally I see that two things so far are relevant.

  1. The contents of an individual cell are shown at 'cellForRowAtIndexPath of the DetailViewController' (TeamViewContoller in my case).

  2. You can include a switch statement in cellForRowAtIndexPath which goes through IndexPath.row and enables you to output a single table row in one page filling out a table view or grouped table view. See this page of the Books example app that Alex so kindly mentioned in his answer. (http://developer.apple.com/iphone/library/samplecode/CoreDataBooks/Listings/Classes_DetailViewController_m.html)

  3. How does the new page get it's data? It's like this.

The current view controller on top of the navigation-controller stack creates the next view controller in the sequence and, before it pushes it onto the stack, sets the data this view controller, acting as data source, needs to populate its table view.

Apple's reference here is useful: http://developer.apple.com/iphone/library/documentation/userexperience/conceptual/TableView_iPhone/TableViewAndDataModel/TableViewAndDataModel.html

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »