Search code examples
swiftcore-datatableviewcell

How would you create a table view that, using a segue, has another table view inside each cell using core data?


So, I am creating this app that involves the creation of routines in a table view. Once you create this routine in the table view, you are then given the option to look inside this routine. Once you look inside it, you are presented with another table view that holds a set of tasks each, all of these created by the user. I am using Core Data, and I'm having problems in my data model assigning the task to the routine that it's in. What happens in the simulator is that all of the tasks that I've created can be seen in all of the routines. This is not the behavior that I want at all, what I am looking for is for each routine to have its own individual tasks. How would you do this? Please keep in mind that I am using Core Data.


Solution

  • First you need to define the relationship between the two entities. In the model editor, ctrl-drag from the Routine entity to the Task entity. This will create a new relationship between the two entities (indicated by the line):

    Model Editor

    Select the newRelationship in the Routine entity, and rename it to "tasks"(in the panel on the right), and change the "Type" to "To Many" - each Routine can have many Tasks. Likewise rename the newRelationship in the Task entity to "routine" (you should also decide whether each Task can belong to many Routines, or only one, and change the "Type" accordingly; I've assumed it will be "To One"):

    Model Editor after changing relationships

    The "Type" is indicated by the arrowhead - single for "To One" and double for "To Many". (You should also look through the CoreData docs and decide what "delete rule" you want.)

    That is the relationship is defined. To simplify populating the relationship, it's worth generating NSManagedObject subclasses for each of your entities. Then you can access their properties (and relationships) using dot notation. The Model Editor can generate the subclass definitions for you - in the Editor menu, "Create NSManagedObject subclass":

    Menu

    Follow through the various dialogs and a .swift file will be created for each Entity. Now to populate the relationship is easy - if you have a Routine object, say myRoutine, and a Task object called myTask, just use

    myTask.routine = myRoutine
    

    This will automatically set the relationship both ways - from myRoutine to myTask and vice versa.

    Now, define a property ("myRoutine") in your second view controller of type Routine. When you segue from your first view controller, set the value of myRoutine to the chosen Routine. Then, after creating any new Task objects, set their routine to myRoutine, as above. Or if you are only displaying the tasks for the chosen routine, you can use myRoutine.tasks (which is a NSSet of all the Task objects for the chosen Routine) as the datasource for the table view in your second view controller.