Sonic Master Sonic Master - 3 months ago 16
Swift Question

Tableview for presenting nested JSON programmatically

I have a JSON tree that represent address. Something like this,

- Address Tree
- Address Tree Deeper
- Address Tree Deeper
- Address Tree Deeper
- Address Tree More Deeper
- Address Tree More Deeper
- Address Tree More Deeper
- Address Tree More Deeper
- Address Tree Deepest
- Address Tree Deepest
- Address Tree Deepest
- Address Tree Deepest
- Address Tree
- Address Tree Deeper
- Address Tree Deeper
- Address Tree Deeper
- Address Tree More Deeper
- Address Tree More Deeper
- Address Tree More Deeper
- Address Tree More Deeper
- Address Tree Deepest
- Address Tree Deepest
- Address Tree Deepest
- Address Tree Deepest


I want to display that tree using table and user could select its child, but I don't know how to represent that. I mean, should we create multiple
ViewController
(and ofcourse its scene in storyboard) then we push every we go deeper into address child? Or maybe I could create
UITableView
programmatically?

Or there are any approach that I can use for that case? I think about reusing viewcontroller but I don't have any clue.

Any help would be appreciated. Thank you so much!

Answer

Create 3 UITableViewControlle‌​rs (since it is the maximum) inside your IB, and push the view controllers accordingly if that dictionary has sub categories in it.

An example how you should do it:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    if(yourDictionary[indexPath.row].yourVariable != nil){
        let nextViewController = self.storyboard?.instantiateViewControllerWithIdentifier("nextViewController") as! ScoutPage
        self.navigationController?.pushViewController(nextViewController, animated: true)
    }
}