Drill down hierarchical data with UITableView

前端 未结 2 372
萌比男神i
萌比男神i 2020-12-08 23:53

I want to build an app to navigate a data hierarchy. The structure of this hierarchy is something like the one I\'m showing in the following image. A branch of the structure

2条回答
  •  失恋的感觉
    2020-12-09 00:25

    To recurse to arbitrary depth with only a single VC painted in storyboard, use storyboard to paint the top level: a navigation controller with a table view controller at it's root. The table vc should be a UITableView subclass, and have a storyboard identifier in storyboard (say, "MyCustomTableVC").

    Give MyCustomTableVC a public property (say, ModelItem *node) that indicates which node in your model it should present.

    Instead of storyboard segues, when the table vc gets didSelectRowAtIndexPath, have it create another instance of itself...

    UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard"
                                                             bundle: nil];
    // Imagine it's called MyCustomTableVC
    MyCustomTableVC *newVC = (MyCustomTableVC*)[mainStoryboard 
                        instantiateViewControllerWithIdentifier: @"MyCustomTableVC"];
    

    Set the newVC model node to the selected item (the item in your model at indexPath.row), and then push to it...

    // making up a lot of stuff about your model here, but hopefully you get the idea...
    newVC.node = [self.model objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:newVC animated:YES];
    

    This approach has the benefit of getting all the navigation controller behavior: animated pushes and pops, back buttons, etc.

提交回复
热议问题