使用 UITableView 向下钻取分层数据 [英] Drill down hierarchical data with UITableView
问题描述
我想构建一个应用来导航数据层次结构.此层次结构的结构类似于我在下图中显示的结构.结构的一个分支可以比其他分支具有更多的级别.
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.
我想用这样的 TableView 深入研究这个结构:
I would like to drill-down this structure with TableViews like this:
A > AA > AAB > AABA
或
A > AB > ABA
我想知道的是,我的故事板中是否只能有一个 TableViewController 并重复使用它来显示每个新级别的数据?我不想在我的故事板中有一堆 tableviewcontrollers
链接在一起,因为我不确定我是否会有 3、4 或 100 级的层次结构.
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.
推荐答案
要递归到任意深度,只需在 storyboard 中绘制单个 VC,请使用 storyboard 绘制顶层:导航控制器,其根为表视图控制器.表 vc 应该是一个 UITableView 子类,并且在故事板中有一个故事板标识符(比如MyCustomTableVC").
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").
为 MyCustomTableVC 提供一个公共属性(例如 ModelItem *node),指示它应该在您的模型中显示哪个节点.
Give MyCustomTableVC a public property (say, ModelItem *node) that indicates which node in your model it should present.
代替storyboard segue,当表vc 获得didSelectRowAtIndexPath
时,让它创建自己的另一个实例...
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"];
将newVC模型节点设置为选中的item(indexPath.row你模型中的item),然后push到...
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.
这篇关于使用 UITableView 向下钻取分层数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!