views:

220

answers:

2

I came upon this blog post the other day and literally have no idea how to implement it, logically.

Any ideas?

I was thinking it involves core data, with 'power' being another entity in a to-many relationship, besides that, I'm lost.

+1  A: 

If you want to implement the view hierarchy, you just start with a navigation project. The Superhero tableview controller is your root controller and the powers tableview is a sub controller.

In your core data model, you would have two entities, a superhero entity and a power entity. Each superhero entity would have a name and then a to-many relationship to power entities. The power entity would have a name and (optionally) a reciprocal to many relationship to all the superhero entities that have the power. (Not really needed but in this case but good practice.)

The superhero tableview datasource would do a fetch for all superhero entities and then populate the text of each cell with the name of each superhero. When the user clicked on a hero, the superhero table view would tell the navcontroller would push the power tableview on the stack and hand the selected superhero entity to the power tableview datasource. The power tableview datasource would then populate the table with with the powers in the relationship with the superhero entity. It would also create power entities as needed an add them to the relationship.

You don't need to use core data for this if the amount of data you are using is small. You could just create a dictionary where each key was a superhero's name and each value was an array of power names. The superhero tableview datasource would populate with the keys and the powers tableview datasource would populate with the individual elements in the value array.

I suggest you start with UINavigationController.

TechZen
That makes sense. What about the actual checking or unchecking of powers in the the power list. I'd assume by checking, it would add the entity to the superhero, and by unchecking it would delete.But how to pre-populate the power list with checked/unchecked status?
monotreme
+1  A: 

You can support the checking and unchecking of "power" cells using an NSMutableSet to keep track of the selected powers. This works whether you use Core Data or some other method to supply the powers data to the UITableView.

For example, in the table's data source implementation:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // do usual stuff here including getting the cell

    // determine the power from the IndexPath.row

    if ([selectedPowerSet member:power])
    {
     cell.accessoryType = UITableViewCellAccessoryCheckmark;
    } else {
     cell.accessoryType = UITableViewCellAccessoryNone;
    }

    return cell;
}

and in the table's delegate (usually the same class) implementation:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // determine the power from the IndexPath.row

    if ([selectedPowerSet member:power]) {
        [selectedPowerSet removeObject:power];
    } else {
        [selectedPowerSet addObject:power];
    }

    [tableView reloadData];
}
gerry3
To store the checked Powers, this logically works. But how to save the checked status of the list. I want to be able to go back into the list and check, uncheck the powers, and have that reflected back in the main view.
monotreme
Well, you need to use the final state of the selectedPowerSet to set your data model appropriately. If you are using Core Data, you would adjust the powers relationship on your Superhero object to match the selectedPowerSet.
gerry3