Drill down hierarchical data with UITableView

半腔热情 提交于 2019-12-17 18:27:17

问题


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 could have more levels than the others.

I would like to drill-down this structure with TableViews like this:

    A > AA > AAB > AABA

or

    A > AB > ABA

What I would like to know is if I can have only one TableViewController in my storyboard and reuse it to display each new level of data? I don't want to have a bunch of tableviewcontrollers linked together in my storyboard because I am not sure if I will have a hierarchy of 3, 4, or 100 levels.


回答1:


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.




回答2:


Sure, you can re-use a UITableView. The UITableView has 2 fundamental pieces, the UITableViewDelegate and the UITableViewDataSource.

UITableViewDelegate controls a lot of the table interaction and some display characteristics (heightForRowAtIndexPath and such). What happens when you select a row. Can you even select a row?

UITableViewDataSource is the well, source of the data. How many sections does the table have? How many rows in each section? What are the content of the cell at Section X, row Y?

You could for instance, use the same UITableView and UITableViewDelegate for each table in your hierarchy, but change the UITableViewDataSource. When you want to change the data source, you might do something like...

if (isSingingBand) {
    self.tableview.dataSource = [TableABBADataSource alloc] init];
} else {
    self.tableview.dataSource = [TableAABDataSource alloc] init];
}
[self.tableview reloadData];

Apple's programming guide on Table Views should answer a lot of questions you might run into how implementation.



来源:https://stackoverflow.com/questions/15115637/drill-down-hierarchical-data-with-uitableview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!