John Durand
John Durand

Reputation: 2024

How do I successfully create a drill-down hierarchy of data between three views?

So far I've successfully created a drill down hierarchy of data between two table views. My firstTableView has the original set of rows, and each row in the firstTableView then segues into the secondTableView data specific to that firstTableView row selected.

To get that data hierarchy, I created a class called Sport. Each sport object had a sportCategory property which I passed to the secondTableView. Here's the code:

class Sport {

    var name: String = "sport name"
    var sportCategories: NSArray = ["variations of selected sport"]

}

let aSport:[Sport] = {
    var basketball = Sport()
    basketball.name = "Basketball"
    basketball.sportCategories = {
        var temp = ["International Basketball","Wheelchair Basketball","Beach Basketball","Deaf Basketball","Dwarf Basketball"]
        temp.sortInPlace(<)
        return temp
        }()

    var golf = Sport()
    golf.name = "Golf"
    golf.sportCategories = {
        var temp = ["Miniature Golf","Dart Golf","Sholf","Disc Golf","Footgolf"]
        temp.sortInPlace(<)
        return temp
        }()

    var football = Sport()
    football.name = "Football"
    football.sportCategories = {
        var temp = ["Flag Football","Indoor Football","Arena Football","Non-Tackle Football","Paper Football"]
        temp.sortInPlace(<)
        return temp
        }()

    var swimming = Sport()
    swimming.name = "Swimming"
    swimming.sportCategories = {
        var temp = ["Competitive Swimming","Synchronized Swimming","Duo Swimming","Relay Swimming"]
        temp.sortInPlace(<)
        return temp
        }()

    return  [basketball,golf,football,swimming]
    }()

Now the issue is, each sportCategory row from the secondTableView must then segue into its own collectionView, with its own set of data specific to that secondTableViewCell row selected. So the order must go

firstTableViewCell -> secondTableViewCell -> collectionView

My issue is I don't know how to extend the class to pass data to its own collectionView. At the moment each row segues into the same collectionView data:

So can I pass data from the firstTableView, all the way to the collectionView accordingly, using Swift?

Upvotes: 1

Views: 285

Answers (1)

danh
danh

Reputation: 62676

It's hard to imagine how to drill down through a 3 level hierarchy when the data is only a two level hierarchy. Lets make a three level hierarchy...

class Sport {
    var name: String = "sport name"
    var sportCategories: NSArray = ["variations of selected sport"]
}

Say the hierarchy is simple (but 3 levels)

All Sports
  |
  |
  ----Basketball
         |
         ---- International Basketball
                    |
                    --- Tibetan Basketball

So we don't want "International Basketball" to be a string, we want it to be an object, like Basketball is...

var tibetanBBall = Sport()
tibetanBBall.name = "Tibetan Basketball"
tibetanBBall.sportCategories = [  ]  // this is a leaf.  it has no kids

var internationalBBall = Sport()
internationalBBall.name = "International Basketball"
internationalBBall.sportCategories = [ tibetanBBall ]  // see? it has kids

var bBall = Sport()
bBall.name = "Basketball"
bBall.sportCategories = [ internationalBBall ]

Notice how sportCategories is never an array of strings? It always contains other Sport objects. That's how a hierarchy can get arbitrarily deep.

Now your logic is: when pushing to the next view controller (probably on prepareForSegue.. the question doesn't mention how you initiate the push), pass the selected sport's sportCategories to the next vc.

Upvotes: 1

Related Questions