TruMan1
TruMan1

Reputation: 36118

Create nested table view or separate page?

I'm trying to create an options page for my iOS app. I have an array of categories like this:

var options = [
    "Location",
    "Calculation Method",
    "Juristic Method",
    "Manual Adjustment",
    "Daylight Saving Time"
]

Then I am loading them up in my view controller like this:

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return options.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = UITableViewCell(style: .Default, reuseIdentifier: "Cell")
    cell.textLabel?.text = options[indexPath.row]

    return cell
}

This give me a single table view perfectly! Now I'm trying to handle the click of each row to bring up a list of options for that category. For example, the Location will have a switch to enable GPS or allow them to select their location drop downs or a map. Calculation Method will bring up a table view of check marks. Daylight Saving Time will bring up a single row with a switch.

My question is what is the best approach for all this? Should I create a dictionary of arrays to hold my options and reuse the table, or should I create a separate view for each category of options? I'm finding conflicting or outdated tutorials on this and I'm also having trouble converting examples from Objective-C. Any help or direction would be greatly appreciated!

Upvotes: 1

Views: 779

Answers (2)

Josh Brown
Josh Brown

Reputation: 53103

I'd suggest creating separate views for each category of options. From your example above, you'd end up with 5 more view controllers: Location, Calculation Methods, Juristic Method, Manual Adjustment, and Daylight Saving Time. And then for your Calculation Methods, since it's a table view of check marks, I'd probably have an array that stores each option inside the CalculationMethodsViewController. Same for the others. If they need other data to display, put it in the new view controllers - not in your original view controller.

Upvotes: 4

atreat
atreat

Reputation: 4413

For your top level of categories, I'd suggest using the sections of a tableview to separate things. Then each row in a section would correspond to the detailed option.

To do this, you could have an array of 'categories' that are to keep the order of sections. This would match the options array you've defined in your question. Alongside this, I would put a Dictionary of sub-category options. For Locations it would look like this:

var options = [ 
    "Location" : ["location-enable-gps", "location-choose-list", "location-choose-map"], 
    "Calculation Method": ...,
    "..."
]

In the above Dictionary, the array values for Location are constants that I've defined. This is because you will be changing the behavior of each cell drastically. Enable GPS can just be a checkbox, Choosing from a list may be better to drilldown/modal (and when selected display selected location), choosing from map may also lend itself to a modal design ("Modal" is when a view controller pops up and then dismisses after an action).

Let me know if this all makes sense, it looks like your options will be very diverse, so unfortunately I don't think there is one method (drill down, collection view) that works best. That being said, its all up to you on how you want your users to experience your app.

Upvotes: 1

Related Questions