views:

41

answers:

3

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.

+1  A: 

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.

Alex Reynolds
Thanks Alex for your comment.I've read Apple's Core Data tutorial.Im having no problem getting the data from Core Data into the First View Controller, being RootViewController in this case.Also Im also fine to open the new controller with a table view and push it onto the navigation stack. (So that Im now faced with an empty table view, when you select an item in the the RootView)Maybe if you can just explain for me conceptually how the TeamViewController knows where its drawing its record from and specifically how I can access the currently selected object which is chosen in RootView.
Evolve
This is why I suggest going though the tutorial, as it shows you how to retain a reference to the managed object context in your second ("Team") view controller, as well as a reference for the selected object, for the purposes of accessing specific data from your Core Data store. You set these properties when you push the second view controller.
Alex Reynolds
Hi Alex. I had another good look at the linked Books tutorial and that has been very helpful.It answers neatly the question. How do I display a single row of data (ie a single Team's details) in a grouped view controller.(Basically with a switch over indexPath.row)Very helpful thankyou.
Evolve
+1  A: 

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;
    }
}
TechZen
A: 

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

Evolve